Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyexiv2出错。如何将元数据应用于jpg图像?_Python_Python Imaging Library_Exif_Pyexiv2 - Fatal编程技术网

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

Python pyexiv2出错。如何将元数据应用于jpg图像?,python,python-imaging-library,exif,pyexiv2,Python,Python Imaging Library,Exif,Pyexiv2,我正在编写一个基于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]

我正在编写一个基于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()


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

我认为您误读了pyexiv2的文档,或者在使用
pip
安装时键入了错误的包名。上的示例没有提到
ImageMetadata
Rational
类。它只修改一本字典

编辑
@psf向我突出显示一个名称不同但使用相同导入的包。在这个包中,我找不到任何要导入的
Rational
类,但有一个类
fracts.fracts

对我来说似乎缺少依赖项。你知道哪一个吗?不,如果我知道,我会留下答案。我的评论是为了帮助你自己找到答案。这篇评论确实如此,但却缺少了一个功能:不幸的是,两者都是以相同的方式导入的。
AttributeError: module 'pyexiv2' has no attribute 'Rational'
AttributeError: module 'pyexiv2' has no attribute 'ImageMetadata'