-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuplicateRemover.py
34 lines (28 loc) · 1.05 KB
/
DuplicateRemover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from PIL import Image
import imagehash
import os
class DuplicateRemover:
def __init__(self, dirname, logger, hash_size=8):
self.dirname = dirname
self.hash_size = hash_size
self.logger = logger
def find_duplicates(self):
"""
Find and Delete Duplicates
"""
fnames = os.listdir(self.dirname)
hashes = {}
duplicates = []
for image in fnames:
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:
duplicates.append(image)
else:
hashes[temp_hash] = image
if len(duplicates) != 0:
space_saved = 0
for duplicate in duplicates:
space_saved += os.path.getsize(os.path.join(self.dirname, duplicate))
os.remove(os.path.join(self.dirname, duplicate))
self.logger.info("{} DUplicate Images succesfully Deleted".format(duplicate))