0

I want to compare two images (.png format) pixel by pixel using selenium in python. Or how could i do it using pillow library.

I have a base image and i get the compare image by taking screenshot of the webpage. I want to compare those two images and assert that they are equal. how can I do it.

Below is what I have tried:

def assert_images_are_equal(base_image, compare_image):
    with open(base_image, 'rb') as f1, open(compare_image, 'rb') as f2:
        base_image_contents = f1.read()
        compare_image_contents = f2.read()
    assert base_image_contents == compare_image_contents

But this doesnt work always. I want to compare pixel by pixel. Could someone help me with this using pillow library or any other library apart from PIL? thanks.

someuser2491
  • 1,848
  • 5
  • 27
  • 63
  • PIL and pillow share a common ancestor. Why did you tag selenium if you want pillow? could you please have a look at the tags? – Daemon Painter Sep 26 '19 at 13:22
  • I think you want to solve whole thing fundamentaly wrong. Are you sure both images have same resolution? Are you interested in absolute similarity? Or you are interested if they are somewhat similar? – Martin Sep 26 '19 at 13:48
  • i am new to programming. i want to assert two images are equal and i am using robot framework with selenium in python. how can i assert two images are equal...what is the most common way to make sure if images look similar. thanks – someuser2491 Sep 26 '19 at 14:44
  • without using python, Just using selenium grid you can do this? If you interested i can share the tools – Zakaria Shahed Sep 26 '19 at 14:52
  • Daemon Painter: How can i use pillow library to compare images. thanks. – someuser2491 Sep 26 '19 at 15:23
  • when you say "this doesn't always work", what do you mean? what sort of errors are you experiencing? as Martin pointed out, there are a lot of minor details that make image comparison very difficult to do, depending on your goals. – Breaks Software Sep 26 '19 at 15:28
  • since i am just reading the file contents and checking if they are equal the image files (png) are not equal. give assertionerror. i want to check if they are similar. – someuser2491 Sep 26 '19 at 15:30
  • how can i use pillow library to get pixel by pixel difference. thanks. – someuser2491 Sep 26 '19 at 15:36
  • Possible duplicate of [How can I quantify difference between two images?](https://stackoverflow.com/questions/189943/how-can-i-quantify-difference-between-two-images) – JeffC Sep 26 '19 at 15:57
  • Post example images – fmw42 Sep 26 '19 at 16:59
  • 1
    You could use Python Wand, which is based upon ImageMagick. It has a compare function for pixel by pixel matching of two images. Or use OpenCV template matching. – fmw42 Sep 27 '19 at 06:07

1 Answers1

0

It is rather difficult to say whether 2 images are the same or similar, because it depends on your definitions of "same" and "similar".

  • You can make a solid red image, save it as a PNG and then save the exact same image again and it could be different because the PNG format contains a timestamp in the image header that may have ticked over to the next second in between saves.

  • You can make a solid red PNG file that is 8-bits deep, and another that is 16-bits deep and you cannot see the difference but the data will be grossly different.

  • You can make a TIF file in Motorola byte order and the same file in Intel byte order. Visually, and in calculations, they will be indistinguishable, but the files will be grossly different.

  • You can make a GIF file that is red and it will look no different from a PNG file but the files will differ.

  • You can make a palette image and a true-colour image and the pixels will be grossly different but they will look identical.

  • You could make a simple black image with a white rectangle in the middle and write it using one JPEG library and it will come out different from the same image written with a different JPEG library, or even a different release version of the same library.

There are many more cases...

One a more helpful note, you may want to look at Perceptual Hashing which tells you if images look pretty similar. One library that does Perceptual Hashing is ImageMagick and it has a Python binding here and here.

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