2015-02-09 55 views
8

我有一个非常简单的代码文件:如何使用枕头将图像粘贴到较大的图像上?

from PIL import Image 
til = Image.new("RGB",(50,50)) 
im = Image.open("tile.png") #25x25 
til.paste(im) 
til.paste(im,(23,0)) 
til.paste(im,(0,23)) 
til.paste(im,(23,23)) 
til.save("testtiles.png") 

然而,当我尝试运行它,我得到以下错误:

Traceback (most recent call last): 
    til.paste(im) 
    File "C:\Python27\lib\site-packages\PIL\Image.py", line 1340, in paste 
    self.im.paste(im, box) 
ValueError: images do not match 

是什么原因造成这个错误?他们都是RGB图像,文档没有说这个错误。

+0

通常当2张图像的模式不匹配时。 – runDOSrun 2015-02-09 10:53:37

回答

13

问题出现在第一次粘贴 - 根据PIL文档(http://effbot.org/imagingbook/image.htm),如果没有传递“box”参数,图像的大小必须匹配。

编辑: 我其实误解了文档,你是对的,它不在那里。但从我在这里尝试的,似乎不通过第二个参数,大小必须匹配。如果要保留第二张图像的大小并将其放置在第一张图像的左上角,请执行以下操作:

... 
til.paste(im,(0,0)) 
... 
+1

我实际上使用枕头,PIL叉:http://pillow.readthedocs.org/然而,这是解决方案。谢谢! – user1796160 2015-02-09 11:53:21