1

I am using the Python exif module to extract coordinates from photos. The methods for doing so are: gps_latitude and gps_longitude.

The output for latitude is: (32.0, 37.0, 37.15) and longitude is: (116.0, 30.0, 37.0)

I extracted the location using another tool as well:

32°37'37.2"N 116°30'37.0"W and 32.6270000, -116.5102778

How can I identify the output format: (32.0, 37.0, 37.15), (116.0, 30.0, 37.0) so I can covert it to perhaps decimal degrees?

from exif import Image

with open('IMG_0252.jpg', 'rb') as image_file:
    my_image = Image(image_file)
    print(my_image.gps_latitude, my_image.gps_longitude)
2

They are in degrees, minutes, seconds (DMS) format.

To convert to decimal degrees, the basic formula is:

degrees + minutes / 60 + seconds / 3600

Note that 'W' longitudes and 'S' latitudes are negative, so you need to use the gps_latitude_ref and gps_longitude_ref properties to determine that. Here's a worked example:

from exif import Image

def dms_to_dd(gps_coords, gps_coords_ref):
    d, m, s =  gps_coords
    dd = d + m / 60 + s / 3600
    if gps_coords_ref.upper() in ('S', 'W'):
        return -dd
    elif gps_coords_ref.upper() in ('N', 'E'):
        return dd
    else:
        raise RuntimeError('Incorrect gps_coords_ref {}'.format(gps_coords_ref))

with open('IMG_0252.jpg', 'rb') as image_file:
    my_image = Image(image_file)

    # gps_latitude = (32.0, 37.0, 37.15)
    # gps_longitude = (116.0, 30.0, 37.0)
    # 
    # gps_latitude_ref = 'N'
    # gps_longitude_ref = 'W'
    
    print(dms_to_dd(my_image.gps_latitude, my_image.gps_latitude_ref))
    print(dms_to_dd(my_image.gps_longitude, my_image.gps_longitude_ref))

    32.62699
    -116.51028
1
  • Fantastic! This is very thorough. Thank you. – Digfield Nov 12 '20 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.