Neo*_*ang 0 python image-processing python-imaging-library pillow
我正在尝试使用 Image.paste 函数将 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));
Run Code Online (Sandbox Code Playgroud)
然后我每次都会收到这个错误:
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1127, in paste
self.im.paste(im, box)
ValueError: images do not match
Run Code Online (Sandbox Code Playgroud)
我非常确定图像的大小是正确的,并且包装图像可以容纳两个图像。避免此错误的唯一方法是使 3 个图像(包装器和 2 个组件)大小相同,并从 (0,0) 粘贴。
我的心有余而力不足,请帮忙!
有两个可能的问题。
你确定你的四元组(0, height, width, textHeight)是正确的吗?它应该是(left, upper, right, lower)像素坐标。在这种情况下,粘贴的图像必须与区域的大小相匹配,我认为这就是您的错误所在。或者,您可以给出一个 2 元组,仅给出要粘贴图片的位置的左上角。请参阅:http : //effbot.org/imagingbook/image.htm
您确定 height、width、textHeight 是ints和不是floats?
你可以尝试这样的事情:
x, y = img1.size
wrapper.paste(textImage,(0,height,x,y))
Run Code Online (Sandbox Code Playgroud)