2012-07-10 98 views
2

我在pygame中有一个200x200px的图像,我想将其切成两半,因此2个图形将为100x200px。之后,我想用屏幕上的一定数量的像素将屏幕上的2个新图像覆盖。图像如何以这种方式分割/裁剪?Pygame中的拆分图像

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

回答

1

我相信你会更好使用PIL,这里http://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) 

现在,这是伪代码,但是这就是我想要做的,大致

+0

显然没有宽度属性。 回溯(最近一次调用最后一次): 文件“C:/Users/User/Desktop/test.py”,第11行, half_rect = pygame.Rect(img.width/2,img.height/2 ) AttributeError:'pygame.Surface'对象没有属性'width' – Eric 2012-07-10 20:25:40

+0

你必须做一些学习,但我会认为完整的代码是'img.get_rect()。width'。查找搜索条件,Pygame Rect和Pygame表面以获取更多信息。起初很奇怪,它变得相当简单。 – TankorSmash 2012-07-10 20:39:06

4

你并不需要创建两个图像,只需使用一个位块传输两次:

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) 
+0

正是我在说什么,但用真实的代码。 +1 – TankorSmash 2012-07-10 19:58:43

+0

由于某些原因,尽管将目标设置为4000x4000,blit目标仍在屏幕外。 回溯(最近通话最后一个): 文件 “C:/Users/User/Desktop/test.py”,第24行,在 screen.blit(IMG,origin2,source_area) 类型错误:无效的目标位置blit – Eric 2012-07-10 20:33:21

+0

@ Alpha15,我做了一个小的编辑,也许这是问题,origin2缺少第二个坐标:'origin2 = origin1 [0] + width + separation,origin1 [1]' – pmoleri 2012-07-11 13:33:56