2

I have a 200x200px image in pygame that I'd like to slice in half, so the 2 shapes would be 100x200px. Afterwards, I'd like to blit the 2 new images on the screen with a set amount of pixels in between them. How would an image be split/cropped in such a way?

EDIT - Fixed! I managed to figure this out by myself from using pygame.transform.chop() and pygame.transform.rotate(). Thanks for the help, though. I know a little more thanks to the help of Tankor Smash.

Eric
  • 99
  • 2
  • 9

3 Answers3

6

You don't need to create two images, just use one and blit it twice:

origin1 = (0,0)
separation = 20

# Load the image
img = pygame.image.load(image_path)

width, height = img.get_width()/2, img.get_height()
origin2 = origin1[0] + width + separation, origin1[1]

# Blit first half
source_area = pygame.Rect((0,0), (width, height))
screen.blit(img, origin1, source_area)

# Blit second half
source_area = pygame.Rect((width,0), (width, height))
screen.blit(img, origin2, source_area)
pmoleri
  • 4,238
  • 1
  • 15
  • 25
  • For some reason the blit destination is outside the screen, despite setting it to 4000x4000. Traceback (most recent call last): File "C:/Users/User/Desktop/test.py", line 24, in screen.blit(img, origin2, source_area) TypeError: invalid destination position for blit – Eric Jul 10 '12 at 20:33
  • @Alpha15, I did a minor edit, perhaps that was the problem, origin2 was missing the second coordinate: ´origin2 = origin1[0] + width + separation, origin1[1]´ – pmoleri Jul 11 '12 at 13:33
  • @pmoleri I edited the question saying I figured it out. Thanks for offering your help though. :) – Eric Jul 12 '12 at 21:24
1

I believe you'd be better using PIL, here http://www.pythonware.com/products/pil/

But if you were to use Pygame, it'd be something along the lines of creating a surface with the image on it, then blitting one half of the surface to a part of the screen, then the other half at another.

#load the image to a surface
img =pygame.image.load(path) 
#create a rect half the width of the image
half_rect = pygame.rect(img.width/2, img.height/2)
#blit first half
main_screen.blit((0,0), rect, img)
#blit second half
main_screen.blit((image.width+10,0),rect, img)

Now that's pseudocode, but that's how I'd do it, roughly

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
  • Apparently there is no width attribute. Traceback (most recent call last): File "C:/Users/User/Desktop/test.py", line 11, in half_rect = pygame.Rect(img.width/2, img.height/2) AttributeError: 'pygame.Surface' object has no attribute 'width' – Eric Jul 10 '12 at 20:25
  • You'd have to do some learning but I would think that the full code was `img.get_rect().width`. Look up the search terms, Pygame Rect and Pygame surface for more info. It's all strange at first, it get gets fairly simple. – TankorSmash Jul 10 '12 at 20:39
0

The other method is to use subsurfaces http://www.pygame.org/docs/ref/surface.html#Surface.subsurface

If you have many sprites on one sheet, you can use it as a spritesheet : http://www.pygame.org/wiki/Spritesheet?parent=CookBook

ninMonkey
  • 7,211
  • 8
  • 37
  • 66