-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexif_parser.py
50 lines (44 loc) · 1.44 KB
/
exif_parser.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
import PIL.Image
from PIL.ExifTags import TAGS
class ExifParser:
def __init__(self, fname):
self.fname = fname
self.exif_data = self.get_exif_data(fname)
def get_exif_data(self, fname):
"Get embedded EXIF data from image file."
ret = {}
try:
img = PIL.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
# PIL.ExifTags.TAGS lacks the FocalLengthIn35Mm definition
ret['FocalLengthIn35Mm'] = exifinfo.get(41989)
except IOError:
return 'Error'
return ret
def focal_length(self):
"Returns the focal length from the EXIF data hash"
focal_length_in_35_mm = self.exif_data.get('FocalLengthIn35Mm')
if focal_length_in_35_mm:
return focal_length_in_35_mm
values = self.exif_data.get('FocalLength')
if values:
a, b = values
if b == 0:
return a
else:
return a / b
else:
return None
def camera(self):
"Returns the camera model"
return self.exif_data.get('Model')
def iso(self):
"Returns the ISO"
return self.exif_data.get('ISOSpeedRatings')
def lens(self):
return self.exif_data.get('LensModel')