Search code examples
pythonpython-3.xpng

Compare (equal or not) 2 .png images in Python


I have 2 .png images loaded in memory (they exist as 'bytes' objects) and I need to tell if they are equal or not. I tried to do a checksum, but it doesn't work, since png image format contains some non-pixel data that makes 2 graphically identical images get different checksums. I just need to compare these images and tell if they are equal or not, I don't need to find similarities or something like this. I will appreciate any help.


Solution

  • You can read your 2 images as numpy arrays with OpenCv s.t.

    import cv2
    image1 = cv2.imread("image1.png")
    image2 = cv2.imread("image2.png")
    if (image1 == image2):
       print("same images")
    else:
       print("not same images")