-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename-heic
More file actions
executable file
·48 lines (30 loc) · 1.04 KB
/
rename-heic
File metadata and controls
executable file
·48 lines (30 loc) · 1.04 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
#!/usr/bin/env python3
import re
import shutil
import sys
from collections import OrderedDict
from PIL import Image
from pillow_heif import register_heif_opener
REGEX = re.compile(r"(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})")
def get_exif_date(filename):
image = Image.open(filename)
image.verify()
return image.getexif()[306]
def try_insert(dict, date, old_filename):
year, month, day, hour, minute, second = date
new_filename = f"{year}-{month}-{day}_{hour}-{minute}-{second}.heic"
i = 0
while new_filename in dict:
new_filename = f"{year}-{month}-{day}_{hour}-{minute}-{second}_{i}.heic"
i += 1
dict[new_filename] = old_filename
if __name__ == "__main__":
register_heif_opener()
files = OrderedDict()
for filename in sys.argv[1:]:
date = get_exif_date(filename)
if m := REGEX.match(date):
try_insert(files, m.groups(), filename)
for i, (new, old) in enumerate(files.items()):
print(f"{i:6d} {old} -> {new}")
shutil.move(old, new)