Skip to content

Instantly share code, notes, and snippets.

@Tendro
Forked from erans/get_lat_lon_exif_pil.py
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save Tendro/792db9dd290852e705ed to your computer and use it in GitHub Desktop.

Select an option

Save Tendro/792db9dd290852e705ed to your computer and use it in GitHub Desktop.
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
def get_exif_data(image):
"""Returns a dictionary from the exif data of an PIL Image item. Also converts the GPS Tags"""
exif_data = {}
info = image._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
if decoded == "GPSInfo":
gps_data = {}
for t in value:
sub_decoded = GPSTAGS.get(t, t)
gps_data[sub_decoded] = value[t]
exif_data[decoded] = gps_data
else:
exif_data[decoded] = value
return exif_data
def _get_if_exist(data, key):
if key in data:
return data[key]
return None
def _convert_to_degress(value):
"""Helper function to convert the GPS coordinates stored in the EXIF to degress in float format"""
d0 = value[0][0]
d1 = value[0][1]
d = float(d0) / float(d1)
m0 = value[1][0]
m1 = value[1][1]
m = float(m0) / float(m1)
s0 = value[2][0]
s1 = value[2][1]
s = float(s0) / float(s1)
return d + (m / 60.0) + (s / 3600.0)
def get_lat_lon(exif_data):
"""Returns the latitude and longitude, if available, from the provided exif_data (obtained through get_exif_data above)"""
lat = None
lon = None
if "GPSInfo" in exif_data:
gps_info = exif_data["GPSInfo"]
gps_latitude = _get_if_exist(gps_info, "GPSLatitude")
gps_latitude_ref = _get_if_exist(gps_info, 'GPSLatitudeRef')
gps_longitude = _get_if_exist(gps_info, 'GPSLongitude')
gps_longitude_ref = _get_if_exist(gps_info, 'GPSLongitudeRef')
if gps_latitude and gps_latitude_ref and gps_longitude and gps_longitude_ref:
lat = _convert_to_degress(gps_latitude)
if gps_latitude_ref != "N":
lat = 0 - lat
lon = _convert_to_degress(gps_longitude)
if gps_longitude_ref != "E":
lon = 0 - lon
return lat, lon
def get_altitude(exif_data):
""" extract altitude if avalaible from the
provided exif_data (obtained through get_exif_data above)"""
alt = None
if "GPSInfo" in exif_data:
gps_info = exif_data["GPSInfo"]
gps_altitude = _get_if_exist(gps_info, "GPSAltitude")
gps_altitude_ref = _get_if_exist(gps_info,'GPSAltitudeRef')
if gps_altitude:
alt = float(gps_altitude[0]) / float(gps_altitude[1])
if gps_altitude_ref == 1:
alt *=-1
return alt
def get_timestamp(exif_data):
""" extract the timestamp if avalaible as datetime from the
provided exif_data (obtained through get_exif_data above)
this is more appropriate for garmin GPSmap 62 camera and other
garmin related photo with geotags. Dirty tricks but need to be
formated a little more"""
dt = exif_data['DateTime']
return dt
################
# Example ######
################
if __name__ == "__main__":
image = ... # load an image through PIL's Image object
exif_data = get_exif_data(image)
print get_lat_lon(exif_data)
@Tendro
Copy link
Author

Tendro commented May 22, 2014

This is to clean up a snippet of python exif extract to meet a specific need for field team with Garmin GPS that has camera . The main goal is to loop through folder with lots of photos and create a .gpx files. For now, I can create a .csv files with the following info (fileName, Lat, Lon, Alt, dateTime) and export to .gpx . Last piece to do that does take an hour : format the output with gpxpy lib to have .gpx ready for different viewing platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment