我正在编写一个基于 python3 的脚本,旨在根据提取的 EXIF 元数据自动将地理标签应用于 .jpg 图像的过程。
PIL 用于打开图像并读取 EXIF 元数据,而 pyexiv2 用于将地理标签应用于 .jpg 图像。
应用地理标签的代码如下所示:
# 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()
我遇到了几个与 pyexiv2 相关的错误,包括
AttributeError: module 'pyexiv2' has no attribute 'Rational'
和
AttributeError: module 'pyexiv2' has no attribute 'ImageMetadata'
对于可以为 .jpg 图像应用/更新元数据的库的任何其他建议,我们也将不胜感激。