Skip to content

Instantly share code, notes, and snippets.

@arminreiter
Last active February 2, 2022 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arminreiter/380e9b64124c0d2227e50af5178c27eb to your computer and use it in GitHub Desktop.
Save arminreiter/380e9b64124c0d2227e50af5178c27eb to your computer and use it in GitHub Desktop.
# 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(" [\033[91m\u2717\033[0m] Images (" + filename + ") are different, store difference in " + outpath)
os.makedirs(outputdir , exist_ok=True)
diff_img.save(outpath)
else:
print(" [\033[92m\N{check mark}\033[0m] Images (" + filename + ") are equal")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment