简体   繁体   English

Python PIL ValueError:图像不匹配

[英]Python PIL ValueError: images do not match

I'm playing around with PIL and came across this problem and I can't see where in the docs I'm going wrong.我正在玩 PIL 并遇到了这个问题,我看不出我在文档中哪里出错了。 Here is my simple code这是我的简单代码

from PIL import Image
from PIL.ImageChops import difference

imageA = Image.open("image1.png")
imageB = Image.open("image2.png")

if imageA.size == imageB.size:
    diff = difference(imageA, imageB)
    diff.save("test.png")

which gives me the error这给了我错误

Traceback (most recent call last):
  File "C:\[XXX]\box-test.py", line 8, in <module>
    diff = difference(imageA, imageB)
  File "C:\Python32\lib\site-packages\PIL\ImageChops.py", line 123, in difference
    return image1._new(image1.im.chop_difference(image2.im))
ValueError: images do not match

Any help would be appreciated任何帮助,将不胜感激

The documentation for this function doesn't tell much in fact. 该功能的文档实际上并没有多少说明。 So let me try to clarify it a little. 所以让我试着澄清一下。 First, the sizes of the images are irrelevant to whether the function works or not, it internally checks for a size that both images fit. 首先,图像的大小与功能是否有效无关,它在内部检查两个图像适合的大小。

Now, when can you actually compare the images by using the function ImageChops.difference ? 现在,您何时可以使用ImageChops.difference函数比较图像?

First, both images have to have pixels that can be stored in an unsigned byte. 首先,两个图像必须具有可以存储在无符号字节中的像素。 This is a very common type of image, but this excludes comparison between images even if they are the same mode. 这是一种非常常见的图像类型,但这排除了图像之间的比较,即使它们是相同的模式。 So, you cannot compare an image x and y when one or /both/ of them have a mode of: F , I , I;16 , I;16L , I;16B , BGR;15 , BGR;16 , BGR;24 , or BGR;32 . 因此,当它们中的一个或两个具有以下模式时,你无法比较图像xyFII;16I;16LI;16BBGR;15BGR;16BGR;24 ,或BGR;32 Just to make it clear: it doesn't matter if both images are in the same mode if they happen to be in one of the modes above, the function will refuse to work. 只是说清楚:如果两个图像碰巧处于上述模式之一,如果两个图像处于相同模式并不重要,则该功能将拒绝工作。

So, the comparison can be done when the images are in the modes 1 , P , L , LA , RGB , RGBA , RGBX , RGBa , CMYK , or YCbCr as long as they have the same number of bands. 因此,当图像处于模式1PLLARGBRGBARGBXRGBaCMYKYCbCr ,可以进行RGBa ,只要它们具有相同数量的频带即可。 This means the images don't have to have the same mode to be compared. 这意味着图像不必具有相同的模式。 For instance, difference(x.convert('CMYK'), x.convert('RGBA')) or difference(x.convert('1'), x.convert('P')) work just fine. 例如, difference(x.convert('CMYK'), x.convert('RGBA'))difference(x.convert('1'), x.convert('P'))工作正常。 Of course this means difference(x.convert('LA'), x.convert('L')) , fails. 当然这意味着difference(x.convert('LA'), x.convert('L'))失败。 Finally, the resulting image will always have the mode equal to the first image passed to the function. 最后,生成的图像将始终具有与传递给函数的第一个图像相同的模式。

This is valid at least for the PIL 1.1.7. 这至少对PIL 1.1.7有效。

只需验证每个图层和图像的每个图像的大小(例如 1024 x 1024)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM