首页 > 解决方案 > How do you compare two images in python to return a numerical difference and time elapsed?

问题描述

  1. numerical difference is expected as positive or negative with decimal places
  2. compare the visual appearance of each image, not their binary contents

for example:
file1.png & file2.gif
diff 0.23
time elapsed 0.843

I have tried


from PIL import Image
from PIL import ImageChops

...

one = Image.open("file1.png")  
two = Image.open("file2.gif")  
diff = ImageChops.difference(one, two)  
print(diff)  

But ImageChops does not work for comparing .gif and .png files. Error is


python3.8/site-packages/PIL/ImageChops.py", line 102, in difference  
    return image1._new(image1.im.chop_difference(image2.im))  
ValueError: images do not match  

Will imagemagick or numpy work? has to support .png, .gif (.jpg, .bmp are optional)

标签: pythonopencvimage-processingimagemagickimage-comparison

解决方案


尝试打印图像的模式。

print(one.mode)
print(two.mode)

如果这两种模式 != 不相等,那么它可能会像您所看到的那样爆炸。


推荐阅读