在Pygame中拆分图像

时间:2012-07-10 19:23:50

标签: python image split pygame blit

我在pygame中有一个200x200px图像,我想将其切成两半,因此2个形状将是100x200px。之后,我想在屏幕上显示2个新图像,并在它们之间设置一定数量的像素。如何以这种方式分割/裁剪图像?

编辑 - 修正了!我设法通过使用pygame.transform.chop()pygame.transform.rotate()来解决这个问题。谢谢你的帮助。感谢Tankor Smash的帮助,我知道了一点。

3 个答案:

答案 0 :(得分:5)

您不需要创建两个图像,只需使用一个图像并将其blit两次:

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)

答案 1 :(得分:1)

我相信您最好使用PILhttp://www.pythonware.com/products/pil/

但是如果你要使用Pygame,它就像创建一个带有图像的表面一样,然后将表面的一半打到屏幕的一部分,然后将另一半打到另一半。

#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)

现在这是伪代码,但我就是这样做的,粗略

答案 2 :(得分:0)

另一种方法是使用subsurface s http://www.pygame.org/docs/ref/surface.html#Surface.subsurface

如果一张纸上有许多精灵,可以将它用作精灵表:http://www.pygame.org/wiki/Spritesheet?parent=CookBook