Python 为什么PIL无法在我的代码中合并2个图像?
我正在尝试使用Image.paste函数将两个图像合并成一个较大的图像。我首先创建一个可以同时保存两个图像的图像,然后粘贴到两个图像中:Python 为什么PIL无法在我的代码中合并2个图像?,python,image-processing,python-imaging-library,pillow,Python,Image Processing,Python Imaging Library,Pillow,我正在尝试使用Image.paste函数将两个图像合并成一个较大的图像。我首先创建一个可以同时保存两个图像的图像,然后粘贴到两个图像中: wrapper = Image.new("I", (width, height+textHeight)); if placement=="bottom": wrapper.paste(img1); wrapper.paste(textImage, (0, height, width, textHeight)); else: wrapper.paste
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粘贴
我束手无策,请帮帮我 有两个可能的问题 您确定您的4元组0、高度、宽度和文本高度正确吗?它应该是左、上、右、下像素坐标。在这种情况下,粘贴的图像必须与区域大小匹配,我认为这就是错误所在。或者,您可以给出一个2元组,仅给出要粘贴图片的左上角。见: 您确定高度、宽度和文本高度是整数而不是浮点数吗 您可以尝试以下方法:
x, y = img1.size
wrapper.paste(textImage,(0,height,x,y))
事实证明,我设置图像大小的方法是错误的:我在textImage上绘制了一些文本,在计算了文本使用的面积后,我需要裁剪出额外的空白。我是用textImage做的。size=w,h。正确的方法是使用裁剪方法。但您的答案提供了我搜索了很多次的两个最常见的错误原因,所以我将其标记为接受答案。谢谢!很高兴知道你明白了。