Free Trial
10 days, no credit_card

<< More Tutorials

How to read GPS coordinates from an image in Python 3 using rethumb?

To read GPS coordinates from an image in Python 3 use the following code:

# -*- encoding: utf-8 -*-

from urllib.request import urlopen
import json

def main():
    response = urlopen("http://api.rethumb.com/v1/exif/all/http://images.rethumb.com/image_exif_1.jpg")

    json_data = json.loads(response.read().decode("utf-8"))

    print(parseGPSCoordinates(json_data))

def parseGPSCoordinates(data):
    if (data['GPS'] is None):
        return 'GPS Coordinates not found';

    values = {};

    values['LAT'] = data['GPS']['GPSLatitudeRef'];    
    values['LONG'] = data['GPS']['GPSLongitudeRef'];
    values['LAT_DEG'] = applyDivision(data['GPS']['GPSLatitude'][0]);
    values['LAT_MIN'] = applyDivision(data['GPS']['GPSLatitude'][1]);
    values['LAT_SEC'] = applyDivision(data['GPS']['GPSLatitude'][2]);
    values['LONG_DEG'] = applyDivision(data['GPS']['GPSLongitude'][0]);
    values['LONG_MIN'] = applyDivision(data['GPS']['GPSLongitude'][1]);
    values['LONG_SEC'] = applyDivision(data['GPS']['GPSLongitude'][2]);

    return format("{LAT} {LAT_DEG}° {LAT_MIN}' {LAT_SEC}'' {LONG} {LONG_DEG}° {LONG_MIN}' {LONG_SEC}''", values);


def applyDivision(value):
    tokens = value.split('/');
    return trim(int(tokens[0]) / float(tokens[1]));

def trim(f):
    if f.is_integer():
        return int(f)
    return f


def format(s, values):
    for key in values:
        s = s.replace('{' + key + '}', str(values[key]));

    return s;

main()

Start using this example now

Use the following commands to get started:

$ git clone https://github.com/rethumb/rethumb-python3-examples.git
$ cd rethumb-python3-examples
$ python3 read-gps-coordinates.js

More examples using Python 3