Created
November 26, 2014 11:40
-
-
Save rafaelcaricio/6d891d0b127f572d3a52 to your computer and use it in GitHub Desktop.
Shows geolocation stored in EXIF of image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
try: | |
from PIL import Image | |
from PIL.ExifTags import TAGS, GPSTAGS | |
except ImportError: | |
print "You need to install PIL. Please check: http://www.pythonware.com/products/pil/" | |
def decode_gps_info(exif): | |
gpsinfo = {} | |
if 'GPSInfo' in exif: | |
for key in exif['GPSInfo'].keys(): | |
decode = GPSTAGS.get(key,key) | |
gpsinfo[decode] = exif['GPSInfo'][key] | |
exif['GPSInfo'] = gpsinfo | |
def get_exif_metadata(image_path): | |
ret = {} | |
image = Image.open(image_path) | |
if hasattr(image, '_getexif'): | |
exifinfo = image._getexif() | |
if exifinfo is not None: | |
for tag, value in exifinfo.items(): | |
decoded = TAGS.get(tag, tag) | |
ret[decoded] = value | |
decode_gps_info(ret) | |
return ret | |
def convert_to_decimal(lon, lat): | |
return (to_decimal(lat[0][0], lat[0][1], | |
lat[1][0], lat[1][1], | |
lat[2][0], lat[2][1]), | |
to_decimal(lon[0][0], lon[0][1], | |
lon[1][0], lon[1][1], | |
lon[2][0], lon[2][1])) | |
def to_decimal(degree_num, degree_den, minute_num, minute_den, | |
second_num, second_den): | |
"""Converts the Degree/Minute/Second formatted GPS data to decimal degrees. | |
Args: | |
degree_num: The numerator of the degree object. | |
degree_den: The denominator of the degree object. | |
minute_num: The numerator of the minute object. | |
minute_den: The denominator of the minute object. | |
second_num: The numerator of the second object. | |
second_den: The denominator of the second object. | |
Returns: | |
A deciminal degree. | |
""" | |
degree = float(degree_num)/float(degree_den) | |
minute = float(minute_num)/float(minute_den)/60 | |
second = float(second_num)/float(second_den)/3600 | |
return degree + minute + second | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Display exif metadata of a image if available.') | |
parser.add_argument('image_path', metavar='image_path', help='Complete image path.') | |
args = parser.parse_args() | |
if args.image_path: | |
for key, value in get_exif_metadata(args.image_path).items(): | |
if key == 'GPSInfo': | |
print convert_to_decimal(value['GPSLongitude'], value['GPSLatitude']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment