1

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. Even I use both ways to compare them it returns true for two different grayscale images.

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:

image1

image2

ghostcasper
  • 99
  • 2
  • 7

1 Answers1

4

Your images have got a superfluous alpha channel. 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:

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

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

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432