How do you compare two images in python to return a numerical difference and time elapsed?

620 Views Asked by At
  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)

1

There are 1 best solutions below

0
On

Try printing the modes of the images.

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

If those two modes != are not equal to each other, then it can bomb out as you are seeing.