I would like to create a program that compares two images. I need to take images from two different folders and compare that images if they are same or not. Then I want to print out as same or different.
For example file 1 will have image1 and image 2 and image 3 , etc then file 2 will have image1,image2 and image3 etc . I need to do this python. How do I do this? Can someone help me? I am new to programming and I am new to python as well.
I have tried the solution as below, but it did not work.
Comparing Image Files
More information.
import math, operator
from PIL import Image
import functools
def compare(file1, file2):
image1 = Image.open(file1)
image2 = Image.open(file2)
h1 = image1.histogram()
h2 = image2.histogram()
rms = math.sqrt( functools.reduce(operator.add,
map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
return rms
if __name__=='__main__':
import sys
file1, file2 = sys.argv[1:]
print( compare(file1, file2))
Actually I am using this for comparing screen taken by automation and manual testing on mobile devices. The files are *.png.
I manage to get this working with below code.
the above code you need top provide the image1 and image 2 on command prompt.But I want pyton to take from imagaes form files one in one location and images from other location and compare autamatiucally. If the images are same then it should print as zero as now the above code respons. If they are different then it will be no zero.
The issue I am have how I could take from two files and compare one by one form scripts.
Eg.
File1\Image1.png ==File2\ image1.png
解决方案
Use ImageMagick, it is available for Python and included on most Linux distros. Get familiar at the commandline first, then work it up into Python.
Create two directories
mkdir directory{1..2}
Create a black square in directory1
convert -size 128x128 xc:black directory1/1.png
Create a black square with a red 10x10 rectangle in directory2
convert -size 128x128 xc:black -fill red -draw "rectangle 0,0, 9,9" directory2/2.png
Now ask ImageMagick to tell us how many pixels are different between the two images, -metric ae is the Absolute Error.
convert directory1/1.png directory2/2.png -metric ae -compare -format "%[distortion]" info:
Output
100
Note 1
If you want to allow the images to be nearly the same, you can add -fuzz 10% which will allow each pixel to differ by up to 10% from the corresponding pixel in the other image before counting it as different. This may be more useful when comparing JPEG images which may have slightly different quality/quantisation settings, and/or anti-aliasing, both of which cause images to differ slightly.
Note 2
You can shell out from Python and run shell scripts like the above using this... link
Note 3
If you create, say a red GIF and a red PNG, and copare them, they will come up identical, like this
# Create red GIF
convert -size 128x128 xc:red red.gif
# Create red PNG
convert -size 128x128 xc:red red.png
# Compare and find no difference
convert red.png red.gif -metric ae -compare -format "%[distortion]" info:
0
despite the fact that the files theselves differ enormously
ls -l red*
-rw-r--r-- 1 mark staff 196 1 Apr 11:52 red.gif
-rw-r--r-- 1 mark staff 290 1 Apr 11:52 red.png