Skip to content

Instantly share code, notes, and snippets.

@akashkahalkar
Created February 1, 2020 17:58
Show Gist options
  • Save akashkahalkar/f0452517d8cb1476be36ba5e1c33ebf5 to your computer and use it in GitHub Desktop.
Save akashkahalkar/f0452517d8cb1476be36ba5e1c33ebf5 to your computer and use it in GitHub Desktop.
Get and parse exif data for an image.
from PIL import Image
from PIL.ExifTags import TAGS
from PIL.ExifTags import GPSTAGS
import pprint
#get exif data from images
def get_exif(filename):
image = Image.open(filename)
image.verify()
return image._getexif()
#converted data into human readable with exif tags
def get_labeled_exif(exif):
labeled = {}
for (key, val) in exif.items():
labeled[TAGS.get(key)] = val
return labeled
#get only gps information
def get_geotagging(exif):
geotagging = {}
for (idx, tag) in TAGS.items():
if tag == 'GPSInfo':
if idx not in exif:
print("No EXIF geotagging found")
for (key, val) in GPSTAGS.items():
if key in exif[idx]:
geotagging[val] = exif[idx][key]
return geotagging
#get DMS value from gps Exif data google search for 'DMS lat long'
def get_decimal_from_dms(dms, ref):
degrees = dms[0][0] / dms[0][1]
minutes = dms[1][0] / dms[1][1] / 60.0
seconds = dms[2][0] / dms[2][1] / 3600.0
if ref in ['S', 'W']:
degrees = -degrees
minutes = -minutes
seconds = -seconds
return round(degrees + minutes + seconds, 5)
def get_coordinates(geotags):
lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef'])
lon = get_decimal_from_dms(geotags['GPSLongitude'], geotags['GPSLongitudeRef'])
return (lat,lon)
exif = get_exif("greenPatta.jpg")
data = get_labeled_exif(exif)
activeTags = ['DateTime', 'DateTimeDigitized', 'ExifImageHeight', 'ExifImageWidth', 'Make', 'Model', 'Software', 'GPSInfo']
dictWithActiveTags = { activeKey: data[activeKey] for activeKey in activeTags }
pprint.pprint(dictWithActiveTags)
@akashkahalkar
Copy link
Author

place image in same directory as script and at line 51 replace "greenPatta.jpg" with image name.

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