I am trying to write a script that adds EXIF GPS data to images using Python. When running the below script, I am getting an error returned from the piexif.dump()
as follows:
(venv) C:\projects\geo-photo>python test2.py
Traceback (most recent call last):
File "C:\projects\geo-photo\test2.py", line 31, in <module>
add_geolocation(image_path, latitude, longitude)
File "C:\projects\geo-photo\test2.py", line 21, in add_geolocation
exif_bytes = piexif.dump(exif_dict)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\projects\geo-photo\venv\Lib\site-packages\piexif\_dump.py", line 74, in dump
gps_set = _dict_to_bytes(gps_ifd, "GPS", zeroth_length + exif_length)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\projects\geo-photo\venv\Lib\site-packages\piexif\_dump.py", line 335, in _dict_to_bytes
length_str, value_str, four_bytes_over = _value_to_bytes(raw_value,
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\projects\geo-photo\venv\Lib\site-packages\piexif\_dump.py", line 244, in _value_to_bytes
new_value += (struct.pack(">L", num) +
struct.error: argument out of range
Does anyone have any idea as to why this would be happening? Below is the full script. Any help appreciated.
import piexif
def add_geolocation(image_path, latitude, longitude):
exif_dict = piexif.load(image_path)
# Convert latitude and longitude to degrees, minutes, seconds format
def deg_to_dms(deg):
d = int(deg)
m = int((deg - d) * 60)
s = int(((deg - d) * 60 - m) * 60)
return ((d, 1), (m, 1), (s, 1))
lat_dms = deg_to_dms(latitude)
lon_dms = deg_to_dms(longitude)
exif_dict["GPS"][piexif.GPSIFD.GPSLatitude] = lat_dms
exif_dict["GPS"][piexif.GPSIFD.GPSLongitude] = lon_dms
exif_dict["GPS"][piexif.GPSIFD.GPSLatitudeRef] = 'N' if latitude >= 0 else 'S'
exif_dict["GPS"][piexif.GPSIFD.GPSLongitudeRef] = 'E' if longitude >= 0 else 'W'
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, image_path)
print("Geolocation data added to", image_path)
# Example usage
latitude = 34.0522 # Example latitude coordinates
longitude = -118.2437 # Example longitude coordinates
image_path = 'test.jpg' # Path to your image
add_geolocation(image_path, latitude, longitude)
from Adding EXIF GPS data to .jpg files using Python and Piexif
No comments:
Post a Comment