-
Notifications
You must be signed in to change notification settings - Fork 0
/
caption-old.py
executable file
·84 lines (59 loc) · 2.05 KB
/
caption-old.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
75
76
77
78
79
80
81
82
83
#!/usr/bin/python
import os
import sys
import cgi
ROOT = "/home3/cburkins/public_html/family/pictures/search/iptcinfo"
sys.path.insert(0, ROOT)
import sys
from PIL import Image, ExifTags
tag_name_to_id = dict([ (v, k) for k, v in ExifTags.TAGS.items() ])
# These I got from reading in files and matching to http://www.exiv2.org/tags.html
# You'll have to map your own if something isn't recognised
tag_name_to_id[270] = 'ImageDescription'
tag_name_to_id[306] = 'DateTime'
tag_name_to_id[256] = 'ImageWidth'
tag_name_to_id[257] = 'ImageLength'
tag_name_to_id[258] = 'BitsPerSample'
tag_name_to_id[40962] = 'PixelXDimension'
tag_name_to_id[40963] = 'PixelYDimension'
tag_name_to_id[305] = 'Software'
tag_name_to_id[37510] = 'UserComment'
tag_name_to_id[40091] = 'XPTitle'
tag_name_to_id[40092] = 'XPComment'
tag_name_to_id[40093] = 'XPAuthor'
tag_name_to_id[40094] = 'XPKeywords'
tag_name_to_id[40095] = 'XPSubject'
tag_name_to_id[40961] = 'ColorSpace' # Bit depth
tag_name_to_id[315] = 'Artist'
tag_name_to_id[33432] = 'Copyright'
def convert_exif_to_dict(exif):
"""
This helper function converts the dictionary keys from
IDs to strings so your code is easier to read.
"""
print "Hello Funciton"
data = {}
if exif is None:
return data
print "Hello Data"
for k,v in exif.items():
print k
if k in tag_name_to_id:
data[tag_name_to_id[k]] = v
else:
data[k] = v
# These fields are in UCS2/UTF-16, convert to something usable within python
for k in ['XPTitle', 'XPComment', 'XPAuthor', 'XPKeywords', 'XPSubject']:
if k in data:
data[k] = data[k].decode('utf-16').rstrip('\x00')
return data
filename = "/home3/cburkins/test.jpg"
im = Image.open(filename)
im.verify()
print "hello world"
exif = convert_exif_to_dict(im._getexif())
if im.format in ['JEPG', 'TIFF']:
print "inside"
exif = convert_exif_to_dict(im._getexif())
print exif['XPTitle']
# ----------------------------------------------------- End ------------------------------------------