-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathremove-duplicates.py
More file actions
executable file
·89 lines (72 loc) · 2.72 KB
/
remove-duplicates.py
File metadata and controls
executable file
·89 lines (72 loc) · 2.72 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
__author__ = ['[Brandon Amos](http://bamos.github.io)']
__date__ = '2015.06.06'
"""
Detect and remove duplicate images using average hashing.
http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
"""
import argparse
import hashlib
import imagehash
import os
from collections import defaultdict
from PIL import Image
def getImgs(d):
"""Get the images from the test directory, partitioned by class."""
exts = ["jpg", "png"]
imgClasses = [] # Images, separated by class.
for subdir, dirs, files in os.walk(d):
imgs = []
for fName in files:
# (imageClass, imageName) = (os.path.basename(subdir), fName)
imageName = fName
if any(imageName.lower().endswith("." + ext) for ext in exts):
imgs.append(os.path.join(subdir, fName))
imgClasses.append(imgs)
return imgClasses
def getHash(imgPath):
"""Get the hash of an image, and catch exceptions if the image
file is corrupted."""
try:
return imagehash.average_hash(Image.open(imgPath))
except:
return None
def runOnClass(args, imgs):
"""Find and remove duplicates within an image class."""
d = defaultdict(list)
for imgPath in imgs:
imgHash = getHash(imgPath)
if imgHash:
d[imgHash].append(imgPath)
numFound = 0
for imgHash, imgs in d.items():
if len(imgs) > 1:
print("{}: {}".format(imgHash, " ".join(imgs)))
numFound += len(imgs) - 1 # Keep a single image.
if args.delete:
largestImg = max(imgs, key=os.path.getsize)
print("Keeping {}.".format(largestImg))
imgs.remove(largestImg)
for img in imgs:
os.remove(img)
if args.sha256:
print("")
for img in imgs:
print(hashlib.sha256(open(img, 'rb').read()).hexdigest())
print("")
return numFound
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('inplaceDir', type=str,
help="Directory of images, divided into "
"subdirectories by class.")
parser.add_argument('--delete', action='store_true',
help="Delete the smallest duplicate images instead "
"of just listing them.")
parser.add_argument('--sha256', action='store_true',
help="Show sha256 sum for duplicate images")
args = parser.parse_args()
numFound = 0
for imgClass in getImgs(args.inplaceDir):
numFound += runOnClass(args, imgClass)
print("\n\nFound {} total duplicate images.".format(numFound))