Comparing JPEG images in PIL

61 Views Asked by At

I am trying to assert equality between JPEG Images using PIL: Currently, I'm relying on exact comparison using the following:

from PIL import Image, ImageChops
img1 = Image.open("tmp_img.jpg")
img2 = Image.open("tmp_img.jpg")
ImageChops.difference(img1, img2).getbbox() is None

This works fine and outputs True for the above example.

The problem is, after saving and loading the image:

img1.save("tmp_img_2.jpg")
img3 = Image.open("tmp_img_2.jpg")
ImageChops.difference(img1, img3).getbbox() is None

It outputs False and I suppose the reason is JPEG compression.

My question is, what metric should I use, in order to assert equality as reliably as possible? I could use MSE as described here. As a threshold I would save and load the image 10 times and see how big the MSE gets after 10 compressions.

But this depends on the specific image I'm using and also feels kind of dumb.

Is there a more generic way to check whether two JPEGs are equal regardless of (repeated) compression?

Thank you

0

There are 0 best solutions below