#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2023/05/21 22:59:29 +0900> """ Compare the directory containing the images, for jpg files only. Usage : python imgdircmp.py DIR1 DIR2 Windows10 x64 22H2 + Python 3.10.6 64bit + ImageHash 4.3.1 """ import sys import os import re from PIL import Image import imagehash def get_files(dirpath): files = {} for file in os.listdir(dirpath): if re.search("\.jpg$", file): path = os.path.join(dirpath, file) if os.path.isfile(path): files[file.lower()] = file return files def main(): if len(sys.argv) != 3: print("Error: usage python script.py DIR1 DIR2") _ = input("\nPush any key") sys.exit() srcdir = sys.argv[1] dstdir = sys.argv[2] print("Dir A: %s" % srcdir) print("Dir B: %s" % dstdir) files_a = get_files(srcdir) files_b = get_files(dstdir) for file in files_a.keys(): if file in files_b: rfile_a = files_a[file] rfile_b = files_b[file] path_a = os.path.join(srcdir, rfile_a) path_b = os.path.join(dstdir, rfile_b) hash_a = imagehash.average_hash(Image.open(path_a)) hash_b = imagehash.average_hash(Image.open(path_b)) if hash_a - hash_b == 0: print("Same image : %s" % rfile_a) else: print("Different image : %s" % rfile_a) _ = input("\nPush any key") if __name__ == '__main__': main()