pyexiv2出错。如何将元数据应用于jpg图像?

2024-06-16 12:57:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个基于python3的脚本,旨在根据提取的EXIF元数据自动将geo标记应用到.jpg图像。在

PIL用于打开图像并读取EXIF元数据,pyexiv2用于将geo标记应用于.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相关的错误,包括

^{pr2}$

以及

AttributeError: module 'pyexiv2' has no attribute 'ImageMetadata'

对于可以为.jpg图像应用/更新元数据的库,任何其他建议也将不胜感激。在


Tags: to图像valueabslocexifrationalmetadata