From 09afeab9177d7aa6221768b986dedc0ae3d705da Mon Sep 17 00:00:00 2001 From: root Date: Sun, 23 Oct 2022 18:52:05 +0200 Subject: [PATCH] fixes script abort in case of an image error #3 --- DuplicateRemover.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/DuplicateRemover.py b/DuplicateRemover.py index 97568e2..dc271ea 100644 --- a/DuplicateRemover.py +++ b/DuplicateRemover.py @@ -9,7 +9,7 @@ class DuplicateRemover: self.dirname = dirname self.hash_size = hash_size - def find_duplicates(self, verbose=True): + def find_duplicates(self, verbose=True, exportCSV=True): """ Find and Delete Duplicates """ @@ -21,14 +21,20 @@ class DuplicateRemover: print("Finding Duplicates Now!\n") for image in fnames: if list(image.lower().split('.'))[-1] in self.extensions: - with Image.open(os.path.join(self.dirname,image)) as img: - temp_hash = imagehash.average_hash(img, self.hash_size) - if temp_hash in hashes: - if verbose: - print("Duplicate {} \nfound for Image {}!\n".format(image,hashes[temp_hash])) - duplicates.append(image) - else: - hashes[temp_hash] = image + try: + with Image.open(os.path.join(self.dirname,image)) as img: + temp_hash = imagehash.average_hash(img, self.hash_size) + if temp_hash in hashes: + if verbose: + print("Duplicate {} \nfound for Image {}!\n".format(image,hashes[temp_hash])) + #if exportCSV: + # a = np.asarray([ image, hashes[temp_hash] ]) + # np.savetxt("duplicates.csv", a, delimiter=",") + duplicates.append(image) + else: + hashes[temp_hash] = image + except Exception as error: + print("Error: The following error occured:\n",image,"\n",error,"\n") if len(duplicates) != 0: if verbose: