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.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: