-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExifReader.py
120 lines (103 loc) · 3.94 KB
/
ExifReader.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
116
117
118
119
120
import imghdr
import os
from PIL import Image
from PIL.ExifTags import TAGS
class ExifReader():
def __init__(self, debug=False):
self.debug = debug
# Recording Times
def get_recordingtimes_from_folder(self, folder):
results = []
for file in os.listdir(folder):
tmp = self.get_recordingtimes_from_file(folder + file)
if tmp:
results.append(tmp)
return results
def get_recordingtimes_from_file(self, file):
return self.get_recordingtimes_from_exifdata(self.get_exifdata_from_file(file))
def get_recordingtimes_from_exifdata(self, data):
results = []
for set in data:
if 'DateTime' in set:
results.append(set['DateTime'])
else:
results.append('Kein Datum')
return results
def get_coordinates_from_file(self, file):
return self.get_coordinates(self.get_exifdata_from_file(file))
def get_coordinates_for_folder(self, folder):
results = []
for file in os.listdir(folder):
try:
if imghdr.what(folder + file):
tmp = self.get_coordinates_from_file(folder + file)
except(PermissionError):
if self.debug:
print('Permission to Subfolder denied')
if tmp:
results.append(tmp)
return results
def get_coordinates_for_exifdata(self, data):
results = []
for set in data:
results.append(self.get_coordinates(set))
return results
def get_coordinates(self, exifdata):
if 'GPSInfo' in exifdata and len(exifdata['GPSInfo']) > 4:
nsdeg = exifdata['GPSInfo'][2][0][0]
nsmin = exifdata['GPSInfo'][2][1][0]
nssec = exifdata['GPSInfo'][2][2][0] / 1000
if exifdata['GPSInfo'][1] == 'S':
south = True
else:
south = False
ewdeg = exifdata['GPSInfo'][4][0][0]
ewmin = exifdata['GPSInfo'][4][1][0]
ewsec = exifdata['GPSInfo'][4][2][0] / 1000
if exifdata['GPSInfo'][3] == 'W':
west = True
else:
west = False
if self.debug:
print(str(nsdeg) + '°' + str(nsmin) + '\'' + str(nssec) + '\"' + exifdata['GPSInfo'][1])
print(str(ewdeg) + '°' + str(ewmin) + '\'' + str(ewsec) + '\"' + exifdata['GPSInfo'][3])
long = ewdeg + ewmin / 60 + ewsec / 3600
lat = nsdeg + nsmin / 60 + nssec / 3600
if west:
long *= -1
if south:
lat *= -1
return [lat, long]
else:
return None
# Exif data
def get_exifdata_from_file(self, file):
try:
img = Image.open(file)
info = img._getexif()
ret = {}
useless = ['MakerNote']
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
if decoded not in useless:
ret[decoded] = value
return ret
except(OSError):
print("Wrong file format. Must be an image file.")
def get_exif_data_from_folders(self, folders):
data = []
for folder in folders:
data.extend(self.get_exif_data_from_folder(folder))
return data
def get_exif_data_from_folder(self, folder):
results = []
for file in os.listdir(folder):
try:
if imghdr.what(folder + file):
tmp = self.get_exifdata_from_file(folder + file)
if tmp:
results.append(tmp)
except(PermissionError):
if self.debug:
print('Permission to Subfolder denied')
return results