# python code to compare images in python

I am working on a project in which at one point I need to compare two images.can anybody please help me with a method to do so..By the way the images are screenshots of a software. i wanted to check if the two images are identical including the numbers and letters displayed in the image(screenshot of software)..

• What do you mean when you say compare? Do you want to see if they are identical? Are you looking for details on how to do this in Python or how to compare images in general? – Guy Sirton Aug 5 '12 at 20:58
• yes i wanted to compare to see if they are identical including the numbers/letters displayed in the software – stallion Aug 6 '12 at 4:53

## 3 Answers

There are following ways to do the proper comparison.

• First is the Root-Mean-Square Difference #

To get a measure of how similar two images are, you can calculate the root-mean-square (RMS) value of the difference between the images. If the images are exactly identical, this value is zero. The following function uses the difference function, and then calculates the RMS value from the histogram of the resulting image.

``````# Example: File: imagediff.py

import ImageChops
import math, operator

def rmsdiff(im1, im2):
"Calculate the root-mean-square difference between two images"

h = ImageChops.difference(im1, im2).histogram()

# calculate rms
return math.sqrt(reduce(operator.add,
map(lambda h, i: h*(i**2), h, range(256))
) / (float(im1.size) * im1.size))
``````
• Another is Exact Comparison #

The quickest way to determine if two images have exactly the same contents is to get the difference between the two images, and then calculate the bounding box of the non-zero regions in this image. If the images are identical, all pixels in the difference image are zero, and the bounding box function returns None.

``````import ImageChops

def equal(im1, im2):
return ImageChops.difference(im1, im2).getbbox() is None
``````
• A "magic number" caused me an issue. I suggest replacing `range(256)` with `range(len(h))` for robustness. – shermy Apr 7 '18 at 3:58
• Small quibble I should mention: the big one-liner (return statement) might be nicer broken out. But if not, I'd personally at least give the lambda param h another name, just for clarity: eg: `map(lambda x, i: x*(i**2), h, range(len(h)))`. Cheers! ;) – shermy Apr 7 '18 at 4:02
• Okay. For RGB palette I got 768 values (256 consecutive for R,G,B). So first, the modulus of 256 is needed to make each channel "click over" to zero in our histogram, else, the weighted frequency of the second colour channel, for example, will be 256 times out! Each successive channel would be +256 further out! Also, I noted the need to transpose h and I. This worked: `return math.sqrt(reduce(operator.add, map(lambda h, i: i%256*(h**2), h, range(len(h))) ) / (float(im1.size) * im1.size))` – shermy Apr 8 '18 at 3:34

I'm maintaining a Python library called pyssim that uses the Structured Similarity (SSIM) method to compare two images.

It doesn't have python bindings, but the perceptualdiff program is also awesome at comparing two images - and quite fast.

• is the pyssim a sort of a fuzzy comparison? what if there was a boxes of text in an image, the positions of boxes are same with another image, but the text is slightly different? What score would it return? does it only consider structures? – user299709 Aug 3 '14 at 21:51
• If the text is different, it will have a lower score. – jterrace Aug 4 '14 at 0:57
• Am I correct to say the 2 images compared needs to be of the same dimension? – Lester Cheung Sep 30 '18 at 14:29
• Yes, you'd have to resize one first so they match. – jterrace Oct 1 '18 at 16:07

I can't give a ready to use answer, but I will point you in (I think) the right direction. A simple way of comparing two images is by making a hash of their binary representations and then see if those hashes are the same. One problem with this is with the hash function you want to use and you must look for one that have low chances of collisions, and the other is that an image file probably has metadata attached to the original binary information, so you will have to look at how to cut off that metadata in order to compare the images only using their binary info. Also, I don't know for sure but probably the binary representation of an image encoded in jpg is different from an image encoded in png, so you should be aware of that.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8856567,"math_prob":0.8874852,"size":4343,"snap":"2019-35-2019-39","text_gpt3_token_len":1079,"char_repetition_ratio":0.11200737,"word_repetition_ratio":0.010596027,"special_character_ratio":0.25995857,"punctuation_ratio":0.12541254,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9827214,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-08-21T02:20:24Z\",\"WARC-Record-ID\":\"<urn:uuid:406b6698-7a8b-4890-abb9-29d35d0f2a57>\",\"Content-Length\":\"158116\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:570529fd-c524-4238-b499-c44726161bfa>\",\"WARC-Concurrent-To\":\"<urn:uuid:48673f25-aee2-4531-bb27-39e6f77afd41>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/11816203/python-code-to-compare-images-in-python\",\"WARC-Payload-Digest\":\"sha1:GB7K4OBJJHFT62HCQ7NQ74ZQROA4FGAW\",\"WARC-Block-Digest\":\"sha1:3HCWC4JSNQ5WUNLMLBHI7UXE5BVA2Z3Z\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-35/CC-MAIN-2019-35_segments_1566027315695.36_warc_CC-MAIN-20190821001802-20190821023802-00023.warc.gz\"}"}