2

I am trying to combine 2 images into a larger one with Image.paste function. I start by creating an image that can hold both images, and then paste in the 2 images:

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

Then I get this error every time:

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

I am very sure that the sizes of the images are correct, and the wrapper image can hold both images. The only way to avoid this error is to make the 3 images (wrapper and 2 components) same size, and paste from (0,0).

I am at my wits' end, please help!

4

1 回答 1

11

有两个可能的问题。

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

  2. 你确定 height、width、textHeight 是ints不是floats

你可以尝试这样的事情:

x, y = img1.size
wrapper.paste(textImage,(0,height,x,y))
于 2013-11-30T10:57:30.270 回答