为什么PIL无法在我的代码中合并2个图像?

时间:2013-11-30 07:12:59

标签: python image-processing python-imaging-library pillow

我正在尝试使用Image.paste函数将2个图像组合成更大的图像。我首先创建一个可以保存两个图像的图像,然后粘贴到2个图像中:

wrapper = Image.new("I", (width, height+textHeight));

if placement=="bottom":
 wrapper.paste(img1); 
 wrapper.paste(textImage, (0, height, width, textHeight));
else:
 wrapper.paste(textImage);
 wrapper.paste(img1, (0,textHeight));

然后我每次都会收到此错误:

 File "C:\Python27\lib\site-packages\PIL\Image.py", line 1127, in paste
    self.im.paste(im, box)
ValueError: images do not match

我非常确定图像的大小是正确的,并且包装图像可以保存两个图像。避免此错误的唯一方法是使3个图像(包装器和2个组件)的大小相同,并从(0,0)粘贴。

我在我的智慧结束,请帮忙!

1 个答案:

答案 0 :(得分:3)

有两个可能的问题。

  1. 您确定4-tuple (0, height, width, textHeight)是否正确?它应该是(left, upper, right, lower)像素坐标。在这种情况下,粘贴的图像必须与区域的大小相匹配,我认为这是您的错误所在。或者,您可以给出一个2元组,只给出要粘贴图片的左上角。请参阅:http://effbot.org/imagingbook/image.htm

  2. 您确定高度,宽度,textHeight是ints而不是floats吗?

  3. 您可以尝试这样的事情:

    x, y = img1.size
    wrapper.paste(textImage,(0,height,x,y))