I am writing a python3-based script designed to automate the process of applying geo-tags to .jpg images based on extracted EXIF metadata.
PIL is used to open images and read the EXIF metadata and pyexiv2 is used to apply geo-tags to the .jpg images.
The code for the applying geotags is shown here:
# Convert decimal coordinates to degrees, minutes, seconds
def to_degree(value, loc):
if value < 0:
loc_value = loc[0]
elif value > 0:
loc_value = loc[1]
else:
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)
# Apply geotags to photos based on converted latitude and longitude
def apply_geotags(photo, lat, lng):
# Convert coordinates into degrees, munutes and seconds
lat_deg = to_degree(lat, ["S", "N"])
lng_deg = to_degree(lng, ["W", "E"])
print(lat_deg)
print(lng_deg)
# Error here:
# AttributeError: module 'pyexiv2' has no attribute 'Rational'
exiv_lat = (pyexiv2.Rational(lat_deg[0]*60+lat_deg[1],60),pyexiv2.Rational(lat_deg[2]*100,6000), pyexiv2.Rational(0, 1))
exiv_lng = (pyexiv2.Rational(lng_deg[0]*60+lng_deg[1],60),pyexiv2.Rational(lng_deg[2]*100,6000), pyexiv2.Rational(0, 1))
print(exiv_lat)
print(exiv_lng)
# Error here:
# AttributeError: module 'pyexiv2' has no attribute 'ImageMetadata'
metadata = pyexiv2.ImageMetadata(photo)
metadata.read()
metadata["Exif.GPSInfo.GPSLatitude"] = exiv_lat
metadata["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3]
metadata["Exif.GPSInfo.GPSLongitude"] = exiv_lng
metadata["Exif.GPSInfo.GPSLongitudeRef"] = lng_deg[3]
metadata["Exif.Image.GPSTag"] = 654
metadata["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
metadata["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'
metadata.write()
I am encountering several errors all related to pyexiv2 including
AttributeError: module 'pyexiv2' has no attribute 'Rational'
and
AttributeError: module 'pyexiv2' has no attribute 'ImageMetadata'
Any other recommendations for libraries that can apply/update metadata for .jpg images would also be appreciated.
I think you misread the documentation of pyexiv2 or you mistype the package name when installing with pip
. The example on the github page does not mention ImageMetadata
or Rational
classes. It only modify a dictionary.
Edit:
@psf highlights to me a package with a different name but using the same import py3exiv2. In this package, I cannot find any Rational
class to import but there is a class fractions.Fraction
for exif metadata.