Fast and efficient way to detect if two images are visually identical in Python

ensnare picture ensnare · Jun 1, 2014 · Viewed 11.9k times · Source

Given two images:

image1.jpg
image2.jpg

What's a fast way to detect if they are visually identical in Python? For example, they may have different EXIF data which would yield different checksums, even though the image data is the same).

Imagemagick has an excellent tool, "identify," that produces a visual hash of an image, but it's very processor intensive.

Answer

RodrigoOlmo picture RodrigoOlmo · Jun 1, 2014

Using PIL/Pillow:

from PIL import Image

im1 = Image.open('image1.jpg')
im2 = Image.open('image2.jpg')

if list(im1.getdata()) == list(im2.getdata()):
    print "Identical"
else:
    print "Different"