-
Notifications
You must be signed in to change notification settings - Fork 2
/
EXIFtoKMZ.py
257 lines (212 loc) · 12.3 KB
/
EXIFtoKMZ.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'''
OG Mini
August 23, 2022
Final Project - EXIF to KMZ Parser
Version 1.0
DFS 510 - Digital Forensics
Purpose:
This program will extract and parse the EXIF data from images in a given folder. This information will
be written to a KMZ file that can be opened in Google Earth. The map will display pins of where the images
where taken. Each pin has details showing the image and all timestamps associated with the image. A lined
path will also be shown between the pins showing the progression of photos.
This script can be useful in creating a timeline of photos mapped to their GPS coordinates. The photos could
be compared to the geography around the location on Google Earth and assist in locating more evidence.
Test Photos obtained from:
https://github.com/ianare/exif-samples/tree/master/jpg/gps
'''
import os
import argparse
from zipfile import ZipFile
from dataclasses import dataclass
from datetime import datetime
from math import trunc
from math import ceil
try:
from exif import Image
exif = True
except:
print("EXIF Library Not Installed - run 'pip install exif'")
exif = False
# dataclass object to store EXIF data and image
@dataclass
class ImageEXIF:
fileName: str
descLong: str
gpsTimestamp: str
gpsLongitude: float
gpsLatitude: float
gpsAltitude: float
# Class to process images
class ImageReader:
def __init__(self, rootPath):
self.rootPath = os.path.abspath(rootPath)
self.resultList = []
self.imagesProcessedCount = 0
# Read in images from folder
def ProcessImages(self):
for root, dirs, files in os.walk(self.rootPath):
for nextFile in files:
fullPath = os.path.join(root, nextFile)
result = self.ExamineImage(fullPath)
if result is True:
self.imagesProcessedCount += 1
return self.resultList
# Check file for EXIF Data. Process it and add information to be processed later. Returns True is successful
def ExamineImage(self, theFile):
if os.path.exists(theFile):
if not os.path.islink(theFile):
if os.path.isfile(theFile):
with open(theFile, 'rb') as imgFile:
imgExif = Image(imgFile)
if (imgExif.has_exif):
fileName = os.path.split(theFile)[1]
# Format expected by KML file for timestamp 2007-01-14T21:05:02Z
dObj = datetime.strptime(imgExif.get('gps_datestamp', '1900:1:1') + ' '
+ str(trunc(imgExif.get('gps_timestamp', (0,0,0))[0])) + ':'
+ str(trunc(imgExif.get('gps_timestamp', (0,0,0))[1])) + ':'
+ str(ceil(imgExif.get('gps_timestamp', (0,0,0))[2])),
'%Y:%m:%d %H:%M:%S')
gpsTimestamp = dObj.strftime('%Y-%m-%dT%H:%M:%SZ')
# Equation to convert Degrees Minutes Seconds to Decimal Degrees for KML DD = d + (min/60) + (sec/3600)
# Convert Longitude
gpsDMSLong = imgExif.get('gps_longitude', (0,0,0))
gpsDDLong = gpsDMSLong[0] + (gpsDMSLong[1]/60) + (gpsDMSLong[2]/3600)
longRef = 1
if (imgExif.get('gps_longitude_ref', 'E') == 'W'):
longRef = -1
gpsLongitude = longRef * gpsDDLong
# Convert Latitude
gpsDMSLat = imgExif.get('gps_latitude', (0,0,0))
gpsDDLat = gpsDMSLat[0] + (gpsDMSLat[1]/60) + (gpsDMSLat[2]/3600)
latRef = 1
if (imgExif.get('gps_latitude_ref', 'N') == 'S'):
latRef = -1;
gpsLatitude = latRef * gpsDDLat
# Altitude
gpsAltitude = imgExif.get('gps_altitude', 0)
# HTML Description
# Display the three possible timestamps
dateTimeOriginal = imgExif.get('datetime', 'N/A')
dateTimeDigitized = imgExif.get('datetime_digitized', 'N/A')
dateGPS = imgExif.get('gps_datestamp', 'N/A')
timeGPS = imgExif.get('gps_timestamp', 'N/A')
deviceMake = imgExif.get('make', 'N/A')
deviceModel = imgExif.get('model', 'N/A')
if (timeGPS != 'N/A'):
timeGPS = str(trunc(imgExif.get('gps_timestamp', (0,0,0))[0])) + ':' + str(trunc(imgExif.get('gps_timestamp', (0,0,0))[1])) + ':' + str(ceil(imgExif.get('gps_timestamp', (0,0,0))[2]))
descLong = '<hr><p>Device Information</p><ul><li>Make - ' + deviceMake + '</li><li>Model - ' + deviceModel + '</li></ul><p>Image Timestamps</p><ul><li>Datetime Original - ' + dateTimeOriginal + '</li><li>Datetime Digitized - ' + dateTimeDigitized + '</li><li>Date GPS - ' + dateGPS + '</li><li>Time GPS - ' + str(timeGPS) + '</li></ul>'
# Add information to list
exifData = ImageEXIF(fileName, descLong, gpsTimestamp, gpsLongitude, gpsLatitude, gpsAltitude)
self.resultList.append(exifData)
return True
else:
print(theFile, 'Skipped no EXIF data')
return False
else:
print(theFile, 'Skipped NOT a File')
return False
else:
print(theFile, 'Skipped Link NOT a File')
return False
else:
print(theFile, 'Path does NOT exist')
return False
# Class to create KML file
class KMLExporter:
def __init__(self, imageList):
self.resultList = imageList
# Creates KML File from the Image List
def KMLExport(self, fileName):
try:
with open(fileName, 'w') as outFile:
# List to store coordinates for path
coordPathList = []
# Start XML
outFile.write('<?xml version="1.0" encoding="UTF-8"?>\n')
outFile.write('<kml xmlns="http://www.opengis.net/kml/2.2">\n')
outFile.write('<Document>\n')
outFile.write('<name>' + fileName + '</name>\n')
# Style red line for path
outFile.write('<Style id="redLine">')
outFile.write('<LineStyle>')
outFile.write('<color>ff0000ff</color>')
outFile.write('<width>4</width>')
outFile.write('</LineStyle>')
outFile.write('</Style>')
# Create individual placemark records from list sorted by gpsTimestamp
for r in sorted(self.resultList, key=lambda itm: itm.gpsTimestamp):
# Add coordinates to list for path
coordPathList.append(str(r.gpsLongitude) + ',' + str(r.gpsLatitude) + ',' + str(r.gpsAltitude))
outFile.write('<Placemark>\n')
outFile.write('<name>' + r.fileName + '</name>\n')
# Format to handle HTML Description <![CDATA[<img style="max-width:500px;" src="files/DSCN0042.jpg">description]]>
descCDATA = '<![CDATA[<img style="max-width:500px;" src="images/' + r.fileName + '">' + r.descLong + ']]>'
outFile.write('<description>' + descCDATA + '</description>\n')
outFile.write('<TimeStamp>\n')
outFile.write('<when>' + r.gpsTimestamp + '</when>\n')
outFile.write('</TimeStamp>\n')
outFile.write('<Point>\n')
outFile.write('<coordinates>' + str(r.gpsLongitude) + ',' + str(r.gpsLatitude) + ',' + str(r.gpsAltitude) + '</coordinates>\n') #'-122.0822035425683,37.42228990140251,0'
outFile.write('</Point>\n')
outFile.write('</Placemark>\n')
# Create red path line showing progression of photos
outFile.write('<Placemark id="track1">')
outFile.write('<name>Photo Path</name>')
outFile.write('<styleUrl>#redLine</styleUrl>')
outFile.write('<LineString><coordinates>')
outFile.write(','.join(coordPathList))
outFile.write('</coordinates></LineString>')
outFile.write('</Placemark>')
# End XML
outFile.write('</Document>\n')
outFile.write('</kml>')
print("\nKML Created: " + fileName + "\n")
except Exception as err:
print("Failed: KML File Save: ",str(err))
# Class to create the KMZ file which is a zip file containing the KML and Assets such as the images
class KMZExporter:
def __init__(self, rootPath, kmlPath):
self.rootPath = os.path.abspath(rootPath)
self.kmlPath = os.path.abspath(kmlPath)
def KMZExport(self):
# https://developers.google.com/kml/documentation/kmzarchives
# Zip KML together with Images to create a KMZ File
with ZipFile(os.path.splitext(self.kmlPath)[0] + '.kmz', 'w') as zipKMZ:
for root, dirs, files in os.walk(self.rootPath):
for nextFile in files:
rootDir = 'images'
filePath = os.path.join(root, nextFile)
parentPath = os.path.relpath(filePath, self.rootPath)
arcName = os.path.join(rootDir, parentPath)
zipKMZ.write(filePath, arcName)
zipKMZ.write(self.kmlPath, os.path.basename(self.kmlPath))
def CheckRootPathDirectory(rootPath):
# Validate the path is a directory
if not os.path.isdir(rootPath):
raise argparse.ArgumentTypeError('Root Path does not exist')
# Validate the path is readable
if os.access(rootPath, os.R_OK):
return rootPath
else:
raise argparse.ArgumentTypeError('Root Path is not readable')
if __name__ == '__main__' and exif:
# Check Arguments / Display help
parser = argparse.ArgumentParser(description='Python EXIF to KMZ Parser v1.0 - This program will extract and parse the EXIF data from images in a given folder. This information will be written to a KMZ file that can be opened in Google Earth. The map will display pins of where the images where taken. Each pin has details showing the image and all timestamps associated with the image. A lined path will also be shown between the pins showing the progression of photos. (ogmini August 2022)')
parser.add_argument('-d', '--rootPath', type= CheckRootPathDirectory,required=True, help="specify the root path for images. Example: C:\ImageFolder")
parser.add_argument('-o', '--output', type= str, required=True, help="specify output KMZ filename. Example: MyMap.kmz")
parsedArguments = parser.parse_args()
# Instantiate ImageReader Object
print("Reading Images from: " + parsedArguments.rootPath)
imageObject = ImageReader(parsedArguments.rootPath)
# Process and Parse Images for EXIF Data
# Instantiate KMLExporter Object
kmlObject = KMLExporter(imageObject.ProcessImages())
print("Images processed: " + str(imageObject.imagesProcessedCount))
outputFilename = os.path.splitext(parsedArguments.output)[0] + '.kml'
# Create KML
kmlObject.KMLExport(outputFilename)
# Instantiate KMZExporter Object
kmzObject = KMZExporter(parsedArguments.rootPath, outputFilename)
# Create KMZ
kmzObject.KMZExport()
print("Process finished")