fixes script abort in case of an image error #3

This commit is contained in:
root 2022-10-23 18:52:05 +02:00
parent fa75d39b66
commit 09afeab917

View File

@ -9,7 +9,7 @@ class DuplicateRemover:
self.dirname = dirname self.dirname = dirname
self.hash_size = hash_size self.hash_size = hash_size
def find_duplicates(self, verbose=True): def find_duplicates(self, verbose=True, exportCSV=True):
""" """
Find and Delete Duplicates Find and Delete Duplicates
""" """
@ -21,14 +21,20 @@ class DuplicateRemover:
print("Finding Duplicates Now!\n") print("Finding Duplicates Now!\n")
for image in fnames: for image in fnames:
if list(image.lower().split('.'))[-1] in self.extensions: if list(image.lower().split('.'))[-1] in self.extensions:
with Image.open(os.path.join(self.dirname,image)) as img: try:
temp_hash = imagehash.average_hash(img, self.hash_size) with Image.open(os.path.join(self.dirname,image)) as img:
if temp_hash in hashes: temp_hash = imagehash.average_hash(img, self.hash_size)
if verbose: if temp_hash in hashes:
print("Duplicate {} \nfound for Image {}!\n".format(image,hashes[temp_hash])) if verbose:
duplicates.append(image) print("Duplicate {} \nfound for Image {}!\n".format(image,hashes[temp_hash]))
else: #if exportCSV:
hashes[temp_hash] = image # 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 len(duplicates) != 0:
if verbose: if verbose: