简体   繁体   English

Python PILLOW 无法正确比较两个图像

[英]Python PILLOW Can't compare two images correctly

I want to compare two images and then decide whether they are the same or not but the PIL library is not able to tell me the correct result.我想比较两个图像,然后确定它们是否相同,但 PIL 库无法告诉我正确的结果。 Even I use both ways to compare them it returns true for two different grayscale images.即使我使用这两种方法来比较它们,它也会为两个不同的灰度图像返回 true。

difference = ImageChops.difference(image1.convert('L'), image2.convert('L'))
if not difference.getbbox() and list(image1.convert('L').getdata()) == list(image2.convert('L').getdata()):

I am using it in this manner but I couldn't handle this problem.我以这种方式使用它,但我无法处理这个问题。

These are the example images for that situation:这些是这种情况的示例图像:

图像1

图2

Your images have got a superfluous alpha channel.您的图像有一个多余的 Alpha 通道。 It appears to work if you discard that.如果您丢弃它,它似乎会起作用。

#!/usr/bin/env python3

from PIL import Image, ImageChops

im1 = Image.open('LQk4R.png').convert('L')
im2 = Image.open('gKx4l.png').convert('L')

diff = ImageChops.difference(im1,im2)
diff.show()

In IPython :IPython中:

In [13]: diff.getbbox() 
Out[13]: (0, 0, 220, 63)

In [14]: im1.getdata() == im2.getdata()
False

在此处输入图像描述

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

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