Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# The following script gets 2 folders as input and
# compares the images with each other. Possible
# differences are stored in a new folder starting
# with compresult_
# usage: python imgCompare.py -f folder1 -s folder2
import argparse
import os
from datetime import datetime
from PIL import Image, ImageChops
parser = argparse.ArgumentParser()
parser.add_argument("--first", "-f", help="path to the first folder for image comparison", required=True)
parser.add_argument("--second", "-s", help="path to the second folder for image comparison", required=True)
args = parser.parse_args()
dir1 = args.first
dir2 = args.second
outputdir = "compresult_" + datetime.now().strftime("%Y%m%d_%H%M%S")
for filename in os.listdir(dir1):
file1 = os.path.join(dir1, filename)
file2 = os.path.join(dir2, filename)
if not os.path.exists(file2):
print("File " + file2 + " does not exist in second folder. Skip image")
continue
im1 = Image.open(file1)
im2 = Image.open(file2)
print("Compare " + filename + " (" + file1 + " AND " + file2 + ")")
diff_img = ImageChops.difference(im1, im2).convert("RGB")
if diff_img.getbbox():
outpath = outputdir + "/" + filename + "-s.png"
print(" Images are different, store difference in " + outpath)
os.makedirs(outputdir , exist_ok=True)
diff_img.save(outpath)
else:
print(" Images are equal")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment