-
Notifications
You must be signed in to change notification settings - Fork 119
/
duplicate_finder.py
366 lines (284 loc) · 10.3 KB
/
duplicate_finder.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python3
"""
A tool to find and remove duplicate pictures.
Usage:
duplicate_finder.py add <path> ... [--db=<db_path>] [--parallel=<num_processes>]
duplicate_finder.py remove <path> ... [--db=<db_path>]
duplicate_finder.py clear [--db=<db_path>]
duplicate_finder.py show [--db=<db_path>]
duplicate_finder.py find [--print] [--delete] [--match-time] [--trash=<trash_path>] [--db=<db_path>]
duplicate_finder.py -h | --help
Options:
-h, --help Show this screen
--db=<db_path> The location of the database or a MongoDB URI. (default: ./db)
--parallel=<num_processes> The number of parallel processes to run to hash the image
files (default: number of CPUs).
find:
--print Only print duplicate files rather than displaying HTML file
--delete Move all found duplicate pictures to the trash. This option takes priority over --print.
--match-time Adds the extra constraint that duplicate images must have the
same capture times in order to be considered.
--trash=<trash_path> Where files will be put when they are deleted (default: ./Trash)
"""
import concurrent.futures
from contextlib import contextmanager
import os
import magic
import math
from pprint import pprint
import shutil
from subprocess import Popen, PIPE, TimeoutExpired
from tempfile import TemporaryDirectory
import webbrowser
from flask import Flask
from flask_cors import CORS
import imagehash
from jinja2 import FileSystemLoader, Environment
from more_itertools import chunked
from PIL import Image, ExifTags
import pymongo
from termcolor import cprint
@contextmanager
def connect_to_db(db_conn_string='./db'):
p = None
# Determine db_conn_string is a mongo URI or a path
# If this is a URI
if 'mongodb://' == db_conn_string[:10] or 'mongodb+srv://' == db_conn_string[:14]:
client = pymongo.MongoClient(db_conn_string)
cprint("Connected server...", "yellow")
db = client.image_database
images = db.images
# If this is not a URI
else:
if not os.path.isdir(db_conn_string):
os.makedirs(db_conn_string)
p = Popen(['mongod', '--dbpath', db_conn_string], stdout=PIPE, stderr=PIPE)
try:
p.wait(timeout=2)
stdout, stderr = p.communicate()
cprint("Error starting mongod", "red")
cprint(stdout.decode(), "red")
exit()
except TimeoutExpired:
pass
cprint("Started database...", "yellow")
client = pymongo.MongoClient()
db = client.image_database
images = db.images
yield images
client.close()
if p is not None:
cprint("Stopped database...", "yellow")
p.terminate()
def get_image_files(path):
"""
Check path recursively for files. If any compatible file is found, it is
yielded with its full path.
:param path:
:return: yield absolute path
"""
def is_image(file_name):
# List mime types fully supported by Pillow
full_supported_formats = ['gif', 'jp2', 'jpeg', 'pcx', 'png', 'tiff', 'x-ms-bmp',
'x-portable-pixmap', 'x-xbitmap']
try:
mime = magic.from_file(file_name, mime=True)
return mime.rsplit('/', 1)[1] in full_supported_formats
except IndexError:
return False
path = os.path.abspath(path)
for root, dirs, files in os.walk(path):
for file in files:
file = os.path.join(root, file)
if is_image(file):
yield file
def hash_file(file):
try:
hashes = []
img = Image.open(file)
file_size = get_file_size(file)
image_size = get_image_size(img)
capture_time = get_capture_time(img)
# hash the image 4 times and rotate it by 90 degrees each time
for angle in [ 0, 90, 180, 270 ]:
if angle > 0:
turned_img = img.rotate(angle, expand=True)
else:
turned_img = img
hashes.append(str(imagehash.phash(turned_img)))
hashes = ''.join(sorted(hashes))
cprint("\tHashed {}".format(file), "blue")
return file, hashes, file_size, image_size, capture_time
except OSError:
cprint("\tUnable to open {}".format(file), "red")
return None
def hash_files_parallel(files, num_processes=None):
with concurrent.futures.ProcessPoolExecutor(max_workers=num_processes) as executor:
for result in executor.map(hash_file, files):
if result is not None:
yield result
def _add_to_database(file_, hash_, file_size, image_size, capture_time, db):
try:
db.insert_one({"_id": file_,
"hash": hash_,
"file_size": file_size,
"image_size": image_size,
"capture_time": capture_time})
except pymongo.errors.DuplicateKeyError:
cprint("Duplicate key: {}".format(file_), "red")
def _in_database(file, db):
return db.count({"_id": file}) > 0
def new_image_files(files, db):
for file in files:
if _in_database(file, db):
cprint("\tAlready hashed {}".format(file), "green")
else:
yield file
def add(paths, db, num_processes=None):
for path in paths:
cprint("Hashing {}".format(path), "blue")
files = get_image_files(path)
files = new_image_files(files, db)
for result in hash_files_parallel(files, num_processes):
_add_to_database(*result, db=db)
cprint("...done", "blue")
def remove(paths, db):
for path in paths:
files = get_image_files(path)
# TODO: Can I do a bulk delete?
for file in files:
remove_image(file, db)
def remove_image(file, db):
db.delete_one({'_id': file})
def clear(db):
db.drop()
def show(db):
total = db.count()
pprint(list(db.find()))
print("Total: {}".format(total))
def same_time(dup):
items = dup['items']
if "Time unknown" in items:
# Since we can't know for sure, better safe than sorry
return True
if len(set([i['capture_time'] for i in items])) > 1:
return False
return True
def find(db, match_time=False):
dups = db.aggregate([{
"$group": {
"_id": "$hash",
"total": {"$sum": 1},
"items": {
"$push": {
"file_name": "$_id",
"file_size": "$file_size",
"image_size": "$image_size",
"capture_time": "$capture_time"
}
}
}
},
{
"$match": {
"total": {"$gt": 1}
}
}])
if match_time:
dups = (d for d in dups if same_time(d))
return list(dups)
def delete_duplicates(duplicates, db):
results = [delete_picture(x['file_name'], db)
for dup in duplicates for x in dup['items'][1:]]
cprint("Deleted {}/{} files".format(results.count(True),
len(results)), 'yellow')
def delete_picture(file_name, db, trash="./Trash/"):
cprint("Moving {} to {}".format(file_name, trash), 'yellow')
if not os.path.exists(trash):
os.makedirs(trash)
try:
shutil.move(file_name, trash + os.path.basename(file_name))
remove_image(file_name, db)
except FileNotFoundError:
cprint("File not found {}".format(file_name), 'red')
return False
except Exception as e:
cprint("Error: {}".format(str(e)), 'red')
return False
return True
def display_duplicates(duplicates, db, trash="./Trash/"):
from werkzeug.routing import PathConverter
class EverythingConverter(PathConverter):
regex = '.*?'
app = Flask(__name__)
CORS(app)
app.url_map.converters['everything'] = EverythingConverter
def render(duplicates, current, total):
env = Environment(loader=FileSystemLoader('template'))
template = env.get_template('index.html')
return template.render(duplicates=duplicates,
current=current,
total=total)
with TemporaryDirectory() as folder:
# Generate all of the HTML files
chunk_size = 25
for i, dups in enumerate(chunked(duplicates, chunk_size)):
with open('{}/{}.html'.format(folder, i), 'w') as f:
f.write(render(dups,
current=i,
total=math.ceil(len(duplicates) / chunk_size)))
webbrowser.open("file://{}/{}".format(folder, '0.html'))
@app.route('/picture/<everything:file_name>', methods=['DELETE'])
def delete_picture_(file_name, trash=trash):
return str(delete_picture(file_name, db, trash))
app.run()
def get_file_size(file_name):
try:
return os.path.getsize(file_name)
except FileNotFoundError:
return 0
def get_image_size(img):
return "{} x {}".format(*img.size)
def get_capture_time(img):
try:
exif = {
ExifTags.TAGS[k]: v
for k, v in img._getexif().items()
if k in ExifTags.TAGS
}
return exif["DateTimeOriginal"]
except:
return "Time unknown"
if __name__ == '__main__':
from docopt import docopt
args = docopt(__doc__)
if args['--trash']:
TRASH = args['--trash']
else:
TRASH = "./Trash/"
if args['--db']:
DB_PATH = args['--db']
else:
DB_PATH = "./db"
if args['--parallel']:
NUM_PROCESSES = int(args['--parallel'])
else:
NUM_PROCESSES = None
with connect_to_db(db_conn_string=DB_PATH) as db:
if args['add']:
add(args['<path>'], db, NUM_PROCESSES)
elif args['remove']:
remove(args['<path>'], db)
elif args['clear']:
clear(db)
elif args['show']:
show(db)
elif args['find']:
dups = find(db, args['--match-time'])
if args['--delete']:
delete_duplicates(dups, db)
elif args['--print']:
pprint(dups)
print("Number of duplicates: {}".format(len(dups)))
else:
display_duplicates(dups, db=db)