forked from zeyus/dir-tree-organizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageutils.py
49 lines (39 loc) · 1.26 KB
/
imageutils.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
import fileutils,time,sys
from PIL import Image
from PIL.ExifTags import TAGS
IMAGE_EXTENSIONS = ['.jpg','.jpeg', '.tiff']
try:
from PIL import Image
except ImportError, e:
exit('PIL module missing %s'%e)
def getCreatedDate(file):
date_ok = False
date = False
try:
exif_data = get_exif(file)
if exif_data.get('DateTimeOriginal', False) and not date_ok:
try:
date = time.mktime(time.strptime(exif_data['DateTimeOriginal'], '%Y:%m:%d %H:%M:%S'))
date_ok = True
except ValueError:
pass
if exif_data.get('DateTime',False) and not date_ok:
try:
date = time.mktime(time.strptime(exif_data['DateTime'], '%Y:%m:%d %H:%M:%S'))
date_ok = True
except ValueError:
pass
except Exception, e:
sys.stdout.write('\nError reading image data from file: %s - Defaulting to file creation time\n'%str(file))
pass
if not date_ok:
return fileutils.getCreatedDate(file)
return date
def get_exif(file):
ret = {}
i = Image.open(file)
info = i._getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
return ret