How to Compare Two Image Files Contents in Python

How to compare two image files contents in python?

If you just want an exact match, you can compare the bytes directly:

def validate_file_contents(file1, file2):
with open(file1, 'rb', errors='ignore') as f1, open(file2, 'rb', errors='ignore') as f2:
contents1 = f1.read()
contents2 = f2.read()
return contents1 == contents2

You could use an assert if you want, but personally I'd check the True/False condition instead.

You also had a few errors in your code:

  1. The content within the with block is not indented.
  2. In a with block you don't need to close() the files.
  3. You are returning a set of content1 and content2, where if they are actually equal, you will only have 1 item returned. You probably wanted to return (content1, content2) as a tuple.

Compare two images the python/linux way

There is a OSS project that uses WebDriver to take screen shots and then compares the images to see if there are any issues (http://code.google.com/p/fighting-layout-bugs/)). It does it by openning the file into a stream and then comparing every bit.

You may be able to do something similar with PIL.

EDIT:

After more research I found

h1 = Image.open("image1").histogram()
h2 = Image.open("image2").histogram()

rms = math.sqrt(reduce(operator.add,
map(lambda a,b: (a-b)**2, h1, h2))/len(h1))

on http://snipplr.com/view/757/compare-two-pil-images-in-python/ and http://effbot.org/zone/pil-comparing-images.htm

How to verify that two images are exactly identical?

How about giving your Images an index?

Pseudocode:

class Frame
{
cvImage img;
uint idx;
}

Than simply check if the current index is greater than the last one you processed.
It is simple and definitely faster than any image processing based approach.

Compare Images in Python

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


Related Topics



Leave a reply



Submit