image - How to compare two imagefile from two different files in python -
i create program compares 2 images. need take images 2 different folders , compare images if same or not. want print out same or different. example file 1 have image1 , image 2 , image 3 , etc file 2 have image1,image2 , image3 etc . need python. how do this? can me? new programming , new python well. have tried solution below, did not work.
import cv2 import numpy 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) f2 in file2: image2 = cv2.imread(f2) difference = cv2.subtract(image1, image2)
result = not np.any(difference) #if difference zeros return false
if result true: print("the images same") else: cv2.imwrite("result.jpg", difference) print ("the images different")
but above code seems not working expected. know loops not correct. new python. can please let me doing wrong here, please?
actually, using comparing screen taken automation , manual testing on mobile devices. files *.png. manage working below code.
the above code need top provide image1 , image 2 on command prompt.but want python take images files 1 in 1 location , images other location , compare automatically. if images same should print 0 above code response. if different no zero. issue facing how take 2 files , compare 1 one scripts. eg. file1\image1.png ==file2\ image1.png
use imagemagick, available python , included on linux distros. familiar @ commandline first, work python.
create 2 directories
mkdir directory{1..2}
create black square in directory1
convert -size 128x128 xc:black directory1/1.png
create black square red 10x10 rectangle in directory2
convert -size 128x128 xc:black -fill red -draw "rectangle 0,0, 9,9" directory2/2.png
now ask imagemagick tell how many pixels different between 2 images, -metric ae
absolute error.
convert directory1/1.png directory2/2.png -metric ae -compare -format "%[distortion]" info:
output
100
note 1
if want allow images same, can add -fuzz 10%
allow each pixel differ 10% corresponding pixel in other image before counting different. may more useful when comparing jpeg images may have different quality/quantisation settings, and/or anti-aliasing, both of cause images differ slightly.
note 2
you can shell out python , run shell scripts above using this... link
note 3
if create, red gif , red png, , copare them, come identical, this
# create red gif convert -size 128x128 xc:red red.gif # create red png convert -size 128x128 xc:red red.png # compare , find no difference convert red.png red.gif -metric ae -compare -format "%[distortion]" info: 0
despite fact 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
Comments
Post a Comment