-
Notifications
You must be signed in to change notification settings - Fork 0
/
exiftools.py
executable file
·115 lines (95 loc) · 3.07 KB
/
exiftools.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
104
105
106
107
108
109
110
111
112
113
114
115
from PIL import Image
from PIL.ExifTags import TAGS
import datetime
from pprint import pprint
from os import listdir, rename, walk
from os.path import isdir, isfile, join, split, splitext
import json
import urllib2
def get_exif(fn):
ret = {}
try:
i = Image.open(fn)
info = i._getexif()
except (IOError, AttributeError):
# Not an image file. Return an empty dict.
print 'Error getting EXIF for file', fn
return ret
if info is None:
return ret
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
return ret
def exif_date_to_ts(exif_date):
# Given a date in exif format, return a unix timestamp
try:
dt = datetime.datetime.strptime(exif_date, '%Y:%m:%d %H:%M:%S')
return int(dt.strftime('%s'))
except:
return None
def get_creation_ts(fn):
exiftags = get_exif(fn)
creation_ts = None
if 'DateTimeOriginal' in exiftags:
creation_ts = exif_date_to_ts(exiftags['DateTimeOriginal'])
elif 'DateTime' in exiftags:
creation_ts = exif_date_to_ts(exiftags['DateTime'])
return creation_ts
def getcoords(d, m, s, ind):
# Calculate the total number of seconds,
# 43'41" = (43*60 + 41) = 2621 seconds.
sec = float((m * 60) + s)
# The fractional part is total number of
# seconds divided by 3600. 2621 / 3600 = ~0.728056
frac = float(sec / 3600)
# Add fractional degrees to whole degrees
# to produce the final result: 87 + 0.728056 = 87.728056
deg = float(d + frac)
# If it is a West or S longitude coordinate, negate the result.
if ind == 'W':
deg = deg * -1
if ind == 'S':
deg = deg * -1
return float(deg)
def getcoords_exif(exif):
if not 'GPSInfo' in exif:
return None
# Latitude
coords = exif['GPSInfo']
# Check that all required fields are present.
has_required_info = all(k in coords for k in [1,2,3,4])
if not has_required_info:
return None
ind = coords[1]
d = float(coords[2][0][0]) / float(coords[2][0][1])
m = float(coords[2][1][0]) / float(coords[2][1][1])
s = float(coords[2][2][0]) / float(coords[2][2][1])
lat = float(getcoords(d, m ,s, ind))
#Longitude
ind = coords[3]
d = float(coords[4][0][0]) / float(coords[4][0][1])
m = float(coords[4][1][0]) / float(coords[4][1][1])
s = float(coords[4][2][0]) / float(coords[4][2][1])
lon = float(getcoords(d, m ,s, ind))
return lat, lon
def locations_from_coords(coords):
url = ('http://maps.googleapis.com/maps/api/geocode/json?latlng=%0.5f,%0.5f&sensor=false' %
(coords[0], coords[1]))
data = urllib2.urlopen(url)
jsondata = json.load(data)
formmatted_locations = []
for result in jsondata['results']:
if 'formatted_address' in result:
formmatted_locations.append(result['formatted_address'])
return formmatted_locations
if __name__ == '__main__':
import sys
topdir = sys.argv[1]
for path, dirs, files in walk(topdir):
for fn in files:
fullname = join(path, fn)
coords = getcoords_exif(get_exif(fullname))
if coords:
locations = locations_from_coords(coords)
print fullname, coords, locations