PIL ValueError: 图像不匹配

5

我一直在使用PIL进行Python编程,正在开发一个将图像镜像到四个象限的函数。显然,我遇到了错误,但似乎无法解决。我的函数如下:

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: Images do not match(图像不匹配)。

我有些迷茫,而且PIL文档也没有帮助我什么。提前感谢您的帮助!


你解决了吗?我遇到了类似的问题,但是无法解决。 - Moondra
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) 这样就可以了,对我来说已经行得通了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接