There are the following methods for correct comparison.
- Firstly, this is the root mean square difference #
To get an idea of โโhow similar the two images are, you can calculate the 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()
- Another is an accurate comparison
The fastest way to determine if two images have exactly the same content is to get the difference between the two images, and then calculate the bounding rectangle of non-zero areas in that image. If the images are identical, all the 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
source share