Skip to content

Instantly share code, notes, and snippets.

Created May 23, 2017 03:00
Show Gist options
  • Save c060604/8a51f8999be12fc2be498e9ca56adc72 to your computer and use it in GitHub Desktop.
Save c060604/8a51f8999be12fc2be498e9ca56adc72 to your computer and use it in GitHub Desktop.
User python and piexif to write some gps info into jpg pictures.
import os
import piexif
from fractions import Fraction
def to_deg(value, loc):
"""convert decimal coordinates into degrees, munutes and seconds tuple
Keyword arguments: value is float gps-value, loc is direction list ["S", "N"] or ["W", "E"]
return: tuple like (25, 13, 48.343 ,'N')
if value < 0:
loc_value = loc[0]
elif value > 0:
loc_value = loc[1]
loc_value = ""
abs_value = abs(value)
deg = int(abs_value)
t1 = (abs_value-deg)*60
min = int(t1)
sec = round((t1 - min)* 60, 5)
return (deg, min, sec, loc_value)
def change_to_rational(number):
"""convert a number to rantional
Keyword arguments: number
return: tuple like (1, 2), (numerator, denominator)
f = Fraction(str(number))
return (f.numerator, f.denominator)
def set_gps_location(file_name, lat, lng, altitude):
"""Adds GPS position as EXIF metadata
Keyword arguments:
file_name -- image file
lat -- latitude (as float)
lng -- longitude (as float)
altitude -- altitude (as float)
lat_deg = to_deg(lat, ["S", "N"])
lng_deg = to_deg(lng, ["W", "E"])
exiv_lat = (change_to_rational(lat_deg[0]), change_to_rational(lat_deg[1]), change_to_rational(lat_deg[2]))
exiv_lng = (change_to_rational(lng_deg[0]), change_to_rational(lng_deg[1]), change_to_rational(lng_deg[2]))
gps_ifd = {
piexif.GPSIFD.GPSVersionID: (2, 0, 0, 0),
piexif.GPSIFD.GPSAltitudeRef: 1,
piexif.GPSIFD.GPSAltitude: change_to_rational(round(altitude)),
piexif.GPSIFD.GPSLatitudeRef: lat_deg[3],
piexif.GPSIFD.GPSLatitude: exiv_lat,
piexif.GPSIFD.GPSLongitudeRef: lng_deg[3],
piexif.GPSIFD.GPSLongitude: exiv_lng,
exif_dict = {"GPS": gps_ifd}
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, file_name)
Copy link

Avi1git commented Jan 11, 2019

Hi, I want to write transformed coordinates into the exif. Values like 3570178.732528623!

breite = exif_dict['GPS'][piexif.GPSIFD.GPSLatitude]
lange = exif_dict['GPS'][piexif.GPSIFD.GPSLongitude]
breite = breite[0][0] / breite[0][1] + breite[1][0] / (breite[1][1] * 60) + breite[2][0] / (breite[2][1] * 3600)
lange = lange[0][0] / lange[0][1] + lange[1][0] / (lange[1][1] * 60) + lange[2][0] / (lange[2][1] * 3600)
x, y = pyproj.transform(wgs84, gk3, lange, breite)

exif_dict['GPS'][piexif.GPSIFD.GPSLatitude] = [ ( (int)(round(y,6) * 1000000), 1000000 ), (0, 1), (0, 1) ]
exif_bytes = piexif.dump(exif_dict)

I am getting struct.error: argument out of range in the dump method. Please advise.

Copy link

bluems commented Apr 8, 2019

@Avi1git Hi.
The Float number your 3570178.732528623 is Rectangular Coordinate System. no GPS.
First, You must study to diffrent GPS and RCS.
You can find if the RCS can not be converted to GPS immediately.

Copy link

CalebM1987 commented Nov 1, 2019

This works nicely to set/update the coordinates, however, you will lose some of the original Exif Data! What should be done is first fetch the original exif data and then just update the GPS tags:

    gps_exif = {"GPS": gps_ifd}

    # get original exif data first!
    exif_data = piexif.load(file_name)

    # update original exif data to include GPS tag
    exif_bytes = piexif.dump(exif_data)

    piexif.insert(exif_bytes, file_name)

See the two images below where the exif properties on the left are done without fetching the original exif data first, while the ones on the right do get the original exif data first and then just updates the gps tags:



Copy link

This was really helpful, thank you. I think there's a small error on line 53. GPSAltitudeRef=1 means below sea level. I guess that's normal for some people, but for most folks you probably want:

        piexif.GPSIFD.GPSAltitudeRef: 0,

Copy link

al13nus commented Feb 1, 2022

How would I add datetime_original or model to the metadata?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment