-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
103 lines (76 loc) · 3.37 KB
/
image.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
# coding: UTF-8
import uuid
from pyexiv2 import ImageMetadata as Metadata
import Image, os
class GPSImage:
def __init__(self, dirname, filename):
self.path = dirname + '/' + filename #improve this using os.utils and concatenate locations
self.filename = filename
self.dirname = dirname
self.id = str(uuid.uuid4())
self.thumb = self.generate_thumbnail()
try:
self.metadata = Metadata(self.path)
self.metadata.read()
except IOError:
print "File " + self.path + " is not a valid image file"
try:
self.lng_tuple = self.metadata['Exif.GPSInfo.GPSLongitude']
self.lat_tuple = self.metadata['Exif.GPSInfo.GPSLatitude']
self.lng = self.coord_to_decimal(self.lng_tuple)
self.lat = self.coord_to_decimal(self.lat_tuple)
self.lng_ref = self.metadata['Exif.GPSInfo.GPSLongitudeRef'].value
self.lat_ref = self.metadata['Exif.GPSInfo.GPSLatitudeRef'].value
if(self.lng_ref == 'W'):
self.lng = - self.lng
if(self.lat_ref == 'S'):
self.lat = - self.lat
except KeyError:
self.lng = 0
self.lat = 0
print "GPS tags not available for " + self.filename
#The EXIF data may optionally embed a thumbnail in the JPEG or TIFF format. The thumbnail can be accessed, set from a JPEG file or buffer, saved to disk and erased:
#thumb = metadata.exif_thumbnail
#thumb.set_from_file('/tmp/thumbnail.jpg')
#thumb.write_to_file('/tmp/copy')
#thumb.erase()
#metadata.write()
def generate_thumbnail(self):
ext = ".jpg"
thumbs_dir = self.create_thumbs_dir()
thumb_img = thumbs_dir + self.filename.split(".")[0] + "_thumb" + ext
if os.path.exists(thumb_img):
print "Thumb for " + self.filename + " found & skipped."
return thumb_img
image = Image.open(self.path)
image = image.resize((128, 128), Image.ANTIALIAS)
image.save(thumb_img)
return thumb_img
def create_thumbs_dir(self):
dir_path = self.dirname + "/.thumbs/"
if not os.path.exists(dir_path):
os.makedirs(dir_path)
return dir_path
def print_gps_data(self):
keys = [
'Exif.GPSInfo.GPSMeasureMode',
'Exif.GPSInfo.GPSVersionID',
'Exif.GPSInfo.GPSLatitudeRef',
'Exif.GPSInfo.GPSLatitude',
'Exif.GPSInfo.GPSLongitudeRef',
'Exif.GPSInfo.GPSLongitude',
'Exif.GPSInfo.GPSTimeStamp',
'Exif.GPSInfo.GPSSatellites'
]
for key in keys:
print self.metadata[key].raw_value
def print_coords(self):
print 'Longitude: ' + str(self.lng)
print 'Latitude: ' + str(self.lat)
print ''
def coord_to_decimal(self, coord):
(degree, minute, second) = coord.value
return (float(degree.numerator)/degree.denominator +
float(minute.numerator)/minute.denominator/60 +
float(second.numerator)/second.denominator/3600)