-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenamer.py
More file actions
71 lines (53 loc) · 2.41 KB
/
renamer.py
File metadata and controls
71 lines (53 loc) · 2.41 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
#!/bin/python3
import argparse
import imagehash
import os
import fnmatch
import glob
from multiprocessing.dummy import Pool as ThreadPool
from PIL import Image
from tqdm import tqdm
from utils.image import apply_rotation_from_original
def rename(path, labels_path = None):
progress = tqdm(range(len(os.listdir(path))), unit="file")
def convert(file_name):
full_path = f"{path}/{file_name}"
extension = full_path.split(".")[-1]
file_name_without_extension = os.path.basename(file_name).split(".")[0]
# normalization and exif cleanup
try:
with Image.open(full_path) as img:
normalized = apply_rotation_from_original(img, img)
if img != normalized:
print (f"{full_path} was normalized")
exif = list(normalized.getdata())
clean_image = Image.new(normalized.mode, normalized.size)
clean_image.putdata(exif)
clean_image.save(full_path)
perceptual_hash = imagehash.phash(normalized)
new_path = f"{path}/{perceptual_hash}.{extension.lower()}"
if new_path != full_path:
print(f"renaming {full_path} to {new_path}")
os.rename(full_path, new_path)
if labels_path != None:
for matching_filename in glob.iglob(f"{labels_path}/**/*{file_name_without_extension}*", recursive=True):
renamed_file_name = matching_filename.replace(file_name_without_extension, f"{perceptual_hash}")
if renamed_file_name != matching_filename:
print(f"renaming match {matching_filename} to {renamed_file_name}")
os.rename(matching_filename, renamed_file_name)
except IOError:
pass
progress.update()
pool = ThreadPool(4)
results = pool.map(convert, fnmatch.filter(os.listdir(path),'*.*'))
pool.close()
pool.join()
progress.close()
def main():
parser = argparse.ArgumentParser(description="Rename all files in directory with hash method")
parser.add_argument("dir", help="Target directory", type=str)
parser.add_argument("labels_dir", nargs='?', help="Labels directory", type=str)
args = parser.parse_args()
rename(args.dir, args.labels_dir)
if __name__ == "__main__":
main()