Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
Created May 9, 2020 21:28
Show Gist options
  • Select an option

  • Save cosinekitty/530e6be514583c77bf6ff8cfa3baf186 to your computer and use it in GitHub Desktop.

Select an option

Save cosinekitty/530e6be514583c77bf6ff8cfa3baf186 to your computer and use it in GitHub Desktop.
Example of extracting GPS coordinates from a jpeg photograph.
#!/usr/bin/env python3
import sys
import exifread # sudo pip3 install exifread
def GpsEval(v):
factor = 1.0
sum = 0.0
for r in v:
sum += factor * (r.num / r.den)
factor /= 60.0
return sum
def GpsInfo(filename):
with open(filename, 'rb') as infile:
tags = exifread.process_file(infile, details=False)
latref = None
lonref = None
lat = None
lon = None
for k,v in tags.items():
if k == 'GPS GPSLatitudeRef':
latref = v.values
elif k == 'GPS GPSLongitudeRef':
lonref = v.values
elif k == 'GPS GPSLatitude':
lat = GpsEval(v.values)
elif k == 'GPS GPSLongitude':
lon = GpsEval(v.values)
if lat is None or lon is None:
return None
if latref == 'S':
lat *= -1.0
if lonref == 'W':
lon *= -1.0
return lat, lon
if __name__ == '__main__':
if len(sys.argv) == 2:
coords = GpsInfo(sys.argv[1])
if coords:
lat, lon = coords
print('{:0.6f} {:0.6f}'.format(lat, lon))
else:
print('No GPS coordinates in image.')
sys.exit(0)
print('USAGE: photogps.py file.jpg')
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment