Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Images in Python

I need to compare two images that are screenshots of a software. I want to check if the two images are identical, including the numbers and letters displayed in the images. How can this be accomplished?

like image 508
stallion Avatar asked Aug 05 '12 11:08

stallion


2 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[0]) * im1.size[1]))
  • 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
like image 139
NIlesh Sharma Avatar answered Oct 03 '22 08:10

NIlesh Sharma


For anyone who stumbles upon this and for whom the accepted answer did not work, I'm posting this here.

I had a similar scenario where I needed to compare one image with thousands of others and find the one that was the closest resembling. I ended up starting off with ImageChop's difference function and applying a mean like so :

import numpy as np

def calcdiff(im1, im2):
    dif = ImageChops.difference(im1, im2)
    return np.mean(np.array(dif))

By turning the difference image into an array I'm able to calculate the mean difference. The lower the difference the more closer the image compared was to the original.


Note: Another approach that worked on near to complete resemblance is to convert the ImageChops.difference(im1, im2) to a numpy array and then to substract exact match pixels [0, 0, 0] to the array. Then by calculating the len() of the array we obtain a score which allows us to differentiate between the images. The closest one being the one with the smallest score

like image 44
Fredaroo Avatar answered Oct 03 '22 09:10

Fredaroo