You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
#!/usr/bin/python3
|
|
|
|
import subprocess
|
|
import itertools
|
|
import os
|
|
import sys
|
|
|
|
|
|
GET_ALL_PICS_LIST_STRING="find . -iname '*.jpg' -o -iname '*.png' -o -iname '*.jpeg'"
|
|
COMPARE_PICS_CMD="compare -metric AE -fuzz 0.1% \"{}\" \"{}\" /dev/null"
|
|
|
|
def main():
|
|
out = subprocess.Popen(['sh', '-c', GET_ALL_PICS_LIST_STRING],
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
all_pics_string = out.communicate()[0].decode('utf-8')
|
|
all_pics = all_pics_string.split('\n')
|
|
print(f"Gathered {len(all_pics)} photos.")
|
|
|
|
duplicate_map = {}
|
|
scanned_files = []
|
|
for pic in all_pics:
|
|
if not pic:
|
|
continue
|
|
name = os.path.basename('.'.join(pic.split('.')[:-1]))
|
|
if not name:
|
|
print("WTF? name is empty")
|
|
continue
|
|
if pic in scanned_files:
|
|
continue
|
|
for other_pic in all_pics:
|
|
if pic == other_pic:
|
|
continue
|
|
if other_pic in scanned_files:
|
|
continue
|
|
if os.path.basename(other_pic).startswith(name):
|
|
if pic not in duplicate_map:
|
|
duplicate_map.update({pic: []})
|
|
scanned_files.append(pic)
|
|
duplicate_map[pic].append(other_pic)
|
|
scanned_files.append(other_pic)
|
|
|
|
origs = len(duplicate_map.keys())
|
|
clones = sum([len(values) for values in duplicate_map.values()])
|
|
candidates = origs + clones
|
|
print(f"Found {origs} + {clones} = {candidates} delete candidate.")
|
|
sys.exit(0)
|
|
|
|
for pic, dups in duplicate_map.items():
|
|
delete_list = []
|
|
pics = [pic] + dups
|
|
for pic_a, pic_b in itertools.combinations(pics, 2):
|
|
if pic_b in delete_list or pic_a in delete_list:
|
|
continue
|
|
if not os.path.exists(pic_a):
|
|
print(f"pic_a doesnt exist: '{pic_a}'")
|
|
continue
|
|
if not os.path.exists(pic_b):
|
|
print(f"pic_b doesnt exist: '{pic_b}'")
|
|
continue
|
|
out = subprocess.Popen(['sh', '-c', COMPARE_PICS_CMD.format(pic_a, pic_b)],
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
value_string, stderr = out.communicate()
|
|
if out.returncode >= 2:
|
|
print(f"HERE!!! stdout: {value_string}\nstderr: {stderr}\nreturn code: {out.returncode}")
|
|
continue
|
|
#diff = float(value_string.decode('utf-8'))
|
|
#if diff == 0.:
|
|
if out.returncode == 0:
|
|
delete_list.append(pic_b)
|
|
|
|
print(f"In list {pics} i will delete")
|
|
print(f"this pics: {delete_list}")
|
|
for delete_elem in delete_list:
|
|
os.remove(delete_elem)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|