-1

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.

import cv2
import numpy as np


file1= "C:\Program Files (x86)\Python35-32\file1" 
file2="C:\Program Files (x86)\Python35-32\file2"

for f1 in file1: image1 = cv2.imread(f1) for f2 in file2: image2 = cv2.imread(f2) difference = cv2.subtract(image1, image2)

result = not np.any(difference) #if difference is all zeros it will return False

if result is True: print("The images are the same") else: cv2.imwrite("result.jpg", difference) print ("the images are different")

but the above code seems to not working as expected. I know the for loops is not correct. I am new to python. Can you please let me what I am doing wrong here, please?

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 the command prompt.But I want python to take from images from files one in one location and images from other location and compare automatically. If the images are same then it should print as zero as now the above code response. If they are different then it will be no zero. The issue I am facing how I could take from two files and compare one by one from scripts. Eg. File1\Image1.png ==File2\ image1.png

  • What kind of images? Do you need to compare them pixel-by-pixel, or can you just check whether the files are identical byte-by-byte? In what way did the other solution "not work"? – DNA Apr 01 '15 at 08:17
  • Didn't work *how*? Show your code and state what errors you got. – TZHX Apr 01 '15 at 08:18
  • So you want to retrieve the files in two different directories ? Look at http://stackoverflow.com/questions/120656. – philant Apr 01 '15 at 09:32
  • yes i want get image file 1(screenshot1.png) from a location (folder 1) and image file2(screenshot2.png) from a location (folder 2) and comapre if the screenshot are same or different. so i will have many screenshot imgage files in both i want to loop through that. – ohmganashayanamaha Apr 01 '15 at 10:31

1 Answers1

1

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

enter image description here

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

enter image description here

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
Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432