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:
try:
with Image.open(os.path.join(self.dirname,image)) as img: with Image.open(os.path.join(self.dirname,image)) as img:
temp_hash = imagehash.average_hash(img, self.hash_size) temp_hash = imagehash.average_hash(img, self.hash_size)
if temp_hash in hashes: if temp_hash in hashes:
if verbose: if verbose:
print("Duplicate {} \nfound for Image {}!\n".format(image,hashes[temp_hash])) 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) duplicates.append(image)
else: else:
hashes[temp_hash] = image 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: