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:
- The content within the
with
block is not indented. - In a
with
block you don't need toclose()
the files. - You are returning a
set
ofcontent1
andcontent2
, where if they are actually equal, you will only have 1 item returned. You probably wanted toreturn (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
Loading All Images Using Imread from a Given Folder
Is There a Memory Efficient and Fast Way to Load Big Json Files
Python Number With 1000 Separator
How to Remove Commas and Dots of Individual Word in Two Dimensional List
Sys.Path Different in Jupyter and Python - How to Import Own Modules in Jupyter
How to Save Training History on Every Epoch in Keras
Run a Python Script from Another Python Script, Passing in Arguments
Arrange a Text File Using Python
Using Buttons in Tkinter to Navigate to Different Pages of the Application
Deleting Dataframe Row in Pandas If a Combination of Column Values Equals a Tuple in a List
Django.Db.Utils.Operationalerror: (1045, Access Denied for User '<User>'@'Localhost'
Numpy Distance Calculations of Different Shaped Arrays
Stripping Whitespaces from a List Inside the List of Tuples
How to Hide Chrome Driver in Python
Get Business Days Between Start and End Date Using Pandas
Exclude First Row When Importing Data from Excel into Python