forked from cloakedcode/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_pictures.py
57 lines (48 loc) · 1.48 KB
/
rename_pictures.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
import os, time
from PIL import Image
from PIL.ExifTags import TAGS
def get_exif_data(fname):
"""Get embedded EXIF data from image file."""
ret = {}
try:
img = Image.open(fname)
if hasattr( img, '_getexif' ):
exifinfo = img._getexif()
if exifinfo != None:
for tag, value in exifinfo.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
except IOError:
print 'IOERROR ' + fname
return ret
def rename_photos(dir):
for file in os.listdir(dir):
f = dir + '/' + file
# If file is a directory, rename the files in it.
if os.path.isdir(f):
rename_photos(f)
continue
# Grab the file extension
(shortname, ext) = os.path.splitext(f)
ftime = ''
# If the file is a jpg
if ext.lower() in ['.jpg', '.jpeg']:
data = get_exif_data(f)
# If the image has a date stamp
if 'DateTime' in data:
ftime = data['DateTime']
elif 'DateTimeOriginal' in data:
ftime = data['DateTimeOriginal']
ftime = ftime.replace(':', '-')
ftime = ftime.replace(' ', '-')
# If time is still empty, use file modification time
if ftime == '':
t = os.path.getmtime(f)
ftime = time.strftime('%Y-%m-%d-%H-%M-%S', time.gmtime(t))
os.rename(f, dir + '/' + ftime + ext)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
rename_photos(sys.argv[1])
else:
print "usage: rename_photos.py directory_of_photos_to_rename_by_timestamp\n"