Comparing two images files from the two folder one by one using python language.
import cv2 import numpy as np #import os
file1= "C:Program Files (x86)Python35-32file1" file2="C:Program Files (x86)Python35-32file2" 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?
You must be logged in to post. Please login or register an account.
Notice how the code you posted here removed your backslashes? in file1 and file2? The backslash is an "escape character" in python. Windows uses them in their directories, and this can cause some trouble.
Thus, either use a double backslash for every backslash in your path (effectively escaping the "value" of the backslash), or use forward slashes. /
-Harrison 8 years ago
Last edited 8 years ago
You must be logged in to post. Please login or register an account.
Thanks, Harrison. I will try this steps and let you know the results.
-selva 8 years ago
You must be logged in to post. Please login or register an account.
heres a working compare and copy script, that copies only similar file names in two folders to a new folder. cheers.
for f1 in file1: if f1.endswith(".png"): image1 = cv2.imread(f1) for f2 in file2: if f2.endswith(".png"): 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")
-selva 8 years ago
Last edited 8 years ago
You must be logged in to post. Please login or register an account.