-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_by_datetime.py
executable file
·74 lines (60 loc) · 2.33 KB
/
rename_by_datetime.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
#!/usr/bin/python
import argparse
from PIL import Image
from PIL.ExifTags import TAGS
import sys
import os
import datetime
import time
import base
import flags
parser = argparse.ArgumentParser(parents=[flags.common_parser])
args = parser.parse_args()
class RenamerByDateTime(object):
"""The class to rename image files based on the date time when the image was
taken."""
def run(self, folder):
"""Runs the functions to rename files.
Args:
folder: str, the folder name.
"""
files = os.listdir(folder)
hash_table = dict()
for eachfile in files:
if not eachfile.lower().endswith('jpg'):
continue
# metadata = pyexiv2.ImageMetadata(os.path.join(folder, eachfile))
# metadata.read()
img = Image.open(os.path.join(folder, eachfile))
exif_data = img._getexif()
timestamp = exif_data[36867]
datetime_obj = datetime.datetime.strptime(timestamp,
'%Y:%m:%d %H:%M:%S')
timestamp = time.mktime(datetime_obj.timetuple())
# if eachfile.lower().startswith('641d'):
# timestamp += 300
if timestamp in hash_table:
hash_table[timestamp].append(eachfile)
else:
hash_table[timestamp] = [eachfile]
keys = hash_table.keys()
keys.sort()
i = 0
for k in keys:
for eachfile in hash_table[k]:
old_name = eachfile.split('.')[0]
new_name = '%03d_%s' % (i, old_name)
old_jpg_name = '%s.JPG' % old_name
new_jpg_name = '%s.JPG' % new_name
print('Rename %s -> %s' % (old_jpg_name, new_jpg_name))
old_cr2_name = '%s.CR2' % old_name
new_cr2_name = '%s.CR2' % new_name
print('Rename %s -> %s' % (old_cr2_name, new_cr2_name))
if not args.dry_run:
os.rename(os.path.join(folder, old_jpg_name),
os.path.join(folder, new_jpg_name))
os.rename(os.path.join(folder, old_cr2_name),
os.path.join(folder, new_cr2_name))
i += 1
if __name__ == '__main__':
base.main(args.folder, RenamerByDateTime())