PIL ValueError:图像不匹配

问题描述 投票:0回答:2

我一直在用 PIL 在 python 中搞乱,我正在研究一个在 4 个象限中镜像和图像的函数。显然我遇到了错误,而且我似乎无法弄清楚。我的功能如下:

def mirror_four(image):
    x = image.size[0]
    y = image.size[1]
    
    temp = Image.new("RGB", (image.size[0], image.size[1]), "black")
    
    tl = image
    tr = mirror_left(image)
    bl = mirror_verticle(image)
    br = mirror_verticle(tr)
    
    image.paste(temp,(0,0,int(x/2),int(y/2)),tl)
    image.paste(temp,(int(x/2),0,0,int(y/2)),tr)
    image.paste(temp,(0,int(y/2),int(x/2),0),bl)
    image.paste(temp,(x/2,y/2,x,y),br)
    
    return temp

这会返回错误:ValueError:图像不匹配

我有点迷失,PIL 文档对我帮助不大。

python image python-imaging-library valueerror
2个回答
4
投票

以您的第一个粘贴行为例 - 对于“paste”的“box”参数,您已指定 (0,0,int(x/2),int(y/2) - 图像大小的一半。但是,尝试粘贴的图像与框的大小不匹配。将“box”参数更改为 (0,0,int(x),int(y)) 将解决您眼前的问题,尽管我怀疑您实际上想要裁剪正在粘贴的图像。

我还要注意,如果您不想提供粘贴图像的大小,则不必提供 - (0,0),因为 x 和 y 也可以。


0
投票

您提供的box参数有误。 应该是

image.paste(the_second_image, (x, y, x+w, y+h)
不要更改最后两个参数。你能做的就是,
w, h = the_second_image.size() image.paste(the_second_image, (x, y, x+w, y+h)
这会起作用,它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.