Extract GPS Latitude and Longitude Data from EXIF using Python Imaging Library (PIL)

I was searching an example of using Python Imaging Library (PIL) to extract the GPS data from EXIF data in images.

There were various half baked examples that didn’t handle things well, so I baked something of my own combining multiple examples.

You can get it here: https://gist.github.com/983821

Or see it embedded below:

  • Marcel Normann

    Very valuable snippet, thanks! I found two bugs in Your code above:

    Line 7: Missing catch
    Line 11ff: Wrong indentation

  • http://eran.sandler.co.il Eran Sandler

    Thanks. This was copy, pasted and edited from the original code :-) I’ll update the gist.

  • Chris Allick

    Oh sweet! Thanks! I needed that helper function! Why don’t they just use long/lat? or at least have both formats?

  • http://eran.sandler.co.il Eran Sandler

    @Chris what do you mean? This is the way to convert the values in EXIF, which are in degrees, minutes and seconds to the decimal format.

    What other formats are you talking about?

  • Chris Allick

    Oh, I meant why not have the decimal format be the standard format for EXIF data?

  • Darus


    Is this free to use as open-source? I am currently in need of such snippet :)

  • http://eran.sandler.co.il/ Eran Sandler

    Sure. It’s free for use under the X11/MIT license (that is, you take this code AS-IS!) :-)

  • Tim Gupta

    This is really awesome! Only suggestion is in _convert_to_degrees(), watch out for the divide by zero issue if degrees, minutes, or seconds are 0. Easy enough to fix though.

    Super helpful!