Python PIL.ExifTags - 不确定这是怎么回事

Python PIL.ExifTags - Not sure what it is all about

我正在阅读我在 GitHub(see code extract below) relating to getting the Latitude and Longitude from EXIF using PIL. I can mostly follow what is happening except for TAGS.get(tags,tags). When I review the Pillow reference material 上找到的一些代码,它提供了一个示例,但不足以让我知道代码正在引入什么或者为什么代码有两个 "tag" 变量显示例如(标签,标签)。如果有人可以阐明这个问题或提供 link 以提供更详细的参考 material,我们将不胜感激。

def get_exif_data(image):
    """Returns a dictionary from the exif data of an PIL Image item. Also converts the GPS Tags"""
    exif_data = {}
    info = image._getexif()
    if info:
        for tag, value in info.items():
            decoded = TAGS.get(tag, tag)
            if decoded == "GPSInfo":
                gps_data = {}
                for t in value:
                    sub_decoded = GPSTAGS.get(t, t)
                    gps_data[sub_decoded] = value[t]

                exif_data[decoded] = gps_data
            else:
                exif_data[decoded] = value

ExifTags.TAGS 是字典。这是整个词典: https://github.com/python-pillow/Pillow/blob/master/src/PIL/ExifTags.py

因此,您可以使用 TAGS.get(key) 获取给定键的值。 如果该键不存在,您可以通过传入第二个参数 TAGS.get(key, val)

为您提供默认值 return

来源: http://www.tutorialspoint.com/python/dictionary_get.htm

get(key[, default]) Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

来源:https://docs.python.org/3/library/stdtypes.html#dict.get

我只是 运行 你的代码。您必须像这样导入另一个模块:from PIL.ExifTags import GPSTAGS。然后就不会运行错误了。