Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting GPS info of exif data from photo in python

I am writing a small program to get the GPS info of a iphone jpg photo.

The library I am using is the PIL in python. Now I am able to get the GPSInfo, which is something like:

{1: 'N', 
 2: ((1, 1), (20, 1), (5365, 100)), 
 3: 'E', 
 4: ((103, 1), (41, 1), (1052, 100)), 
 5: 0, 
 6: (43, 1), 
 7: ((15, 1), (32, 1), (7, 1)), 
 16: 'T', 
 17: (77473, 452), 
 29: '2013:10:25'}

How can I interpret this? And I notice the tag is not continuous, so is there any cheating sheet which I can refer to in order to get a better understanding of all the number tags and what they mean? Thank you!

UPDATES

Sorry, I have figured it out. In the PIL lib, there is a GPSTAGS.get() function which can help me decode the key in gps info. Thank you guys!

gpsinfo = {}
for key in exif['GPSInfo'].keys():
    decode = ExifTags.GPSTAGS.get(key,key)
    gpsinfo[decode] = exif['GPSInfo'][key]
print gpsinfo

and here is the result

{'GPSTimeStamp': ((15, 1), (32, 1), (7, 1)), 
 'GPSImgDirectionRef': 'T', 
 'GPSImgDirection': (77473, 452), 
 'GPSLongitude': ((103, 1), (41, 1), (1052, 100)), 
 'GPSLatitudeRef': 'N', 29: '2013:10:25', 
 'GPSAltitude': (43, 1), 
 'GPSLatitude': ((1, 1), (20, 1), (5365, 100)), 
 'GPSLongitudeRef': 'E', 
 'GPSAltitudeRef': 0}
like image 767
fyr91 Avatar asked Nov 06 '13 05:11

fyr91


People also ask

How do I get GPS coordinates from EXIF data in Python?

To get access to the GPS tags, you need to import GPSTAGS from PIL. ExifTags. Then after parsing the regular tags from the file, you add a second loop to look for the "GPSInfo" tag. If that's present, then you have GPS tags that you can extract.

How do I get GPS metadata from a picture?

In Windows, all you have to do is right-click a picture file, select “Properties,” and then click the “Details” tab in the properties window. Look for the Latitude and Longitude coordinates under GPS.

Does EXIF data include GPS?

Exchangeable Image File Format (EXIF) is a standard that defines specific information related to an image or other media captured by a digital camera. It is capable of storing such important data as camera exposure, date/time the image was captured, and even GPS location.


3 Answers

Use exifread module.

Here is a very helpful gist

import exifread as ef   # barrowed from  # https://gist.github.com/snakeye/fdc372dbf11370fe29eb  def _convert_to_degress(value):     """     Helper function to convert the GPS coordinates stored in the EXIF to degress in float format     :param value:     :type value: exifread.utils.Ratio     :rtype: float     """     d = float(value.values[0].num) / float(value.values[0].den)     m = float(value.values[1].num) / float(value.values[1].den)     s = float(value.values[2].num) / float(value.values[2].den)      return d + (m / 60.0) + (s / 3600.0)   def getGPS(filepath):     '''     returns gps data if present other wise returns empty dictionary     '''     with open(filepath, 'rb') as f:         tags = ef.process_file(f)         latitude = tags.get('GPS GPSLatitude')         latitude_ref = tags.get('GPS GPSLatitudeRef')         longitude = tags.get('GPS GPSLongitude')         longitude_ref = tags.get('GPS GPSLongitudeRef')         if latitude:             lat_value = _convert_to_degress(latitude)             if latitude_ref.values != 'N':                 lat_value = -lat_value         else:             return {}         if longitude:             lon_value = _convert_to_degress(longitude)             if longitude_ref.values != 'E':                 lon_value = -lon_value         else:             return {}         return {'latitude': lat_value, 'longitude': lon_value}     return {}   file_path = 'file path of the file'     gps = getGPS(file_path) print gps                        
like image 84
Venkatesh Mondi Avatar answered Sep 21 '22 15:09

Venkatesh Mondi


Late answer, but as of 2021 you can use GPSPhoto, i.e.:

from GPSPhoto import gpsphoto # Get the data from image file and return a dictionary data = gpsphoto.getGPSData('IMG_20181224_201933.jpg') print(data['Latitude'], data['Longitude']) 

Output:

38.71615498471598 -9.148730635643007


Installation:

pip3 install piexif pip3 install gpsphoto 
like image 30
Pedro Lobito Avatar answered Sep 19 '22 15:09

Pedro Lobito


OP, has already posted a solution using PIL. If you wants to just get GPS info from Python, you can get it by using exifread

Install package using pip

$ pip install exifread

and get GPS data

In [10]: import exifread

In [11]: tags = exifread.process_file(open('./tests/demo-project/content/test.jpg', 'rb'))                                              

In [12]: geo = {i:tags[i] for i in tags.keys() if i.startswith('GPS')}

In [13]: geo
Out[13]: 
{'GPS GPSAltitude': (0x0006) Ratio=186188/239 @ 898,
 'GPS GPSAltitudeRef': (0x0005) Byte=0 @ 722,
 'GPS GPSDate': (0x001D) ASCII=2015:12:06 @ 954,
 'GPS GPSDestBearing': (0x0018) Ratio=43771/526 @ 946,
 'GPS GPSDestBearingRef': (0x0017) ASCII=T @ 806,
 'GPS GPSImgDirection': (0x0011) Ratio=43771/526 @ 938,
 'GPS GPSImgDirectionRef': (0x0010) ASCII=T @ 782,
 'GPS GPSLatitude': (0x0002) Ratio=[46, 3803/100, 0] @ 850,
 'GPS GPSLatitudeRef': (0x0001) ASCII=N @ 674,
 'GPS GPSLongitude': (0x0004) Ratio=[13, 2429/100, 0] @ 874,
 'GPS GPSLongitudeRef': (0x0003) ASCII=E @ 698,
 'GPS GPSSpeed': (0x000D) Ratio=139/50 @ 930,
 'GPS GPSSpeedRef': (0x000C) ASCII=K @ 758,
 'GPS GPSTimeStamp': (0x0007) Ratio=[10, 37, 33] @ 906,
 'GPS Tag 0x001F': (0x001F) Ratio=30 @ 966}
like image 20
Pandikunta Anand Reddy Avatar answered Sep 20 '22 15:09

Pandikunta Anand Reddy