2015-08-19 29 views
1

我通过一些代码,我就发现GitHub阅读(见下面的代码片段)关于获得使用PIL从EXIF的经度和纬度。除了TAGS.get(标签,标签)之外,我大多可以关注发生的事情。当我回顾Pillow reference material,它给出了一个例子,但还不足以让我知道什么代码在拉动或为什么代码有两个“标签”可变因素表明,如(标签,标签)。如果有人能够解释这个问题或提供更详细的参考资料的链接,将非常感激。Python的PIL.ExifTags - 不知道它是所有关于

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 

回答

1

ExifTags.TAGS是一本词典。这里是全字典: https://github.com/python-pillow/Pillow/blob/master/PIL/ExifTags.py

因此,您可以通过使用TAGS.get(key)来获得给定密钥的值。 如果该键不存在,你可以把它用在第二个参数传递TAGS.get(key, val)

来源返回到您的默认值: http://www.tutorialspoint.com/python/dictionary_get.htm

GET(键[默认])返回值如果键位于 字典中,则为key,否则为默认值。如果未给出默认值,则默认为 无,以便此方法不会引发KeyError。

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

相关问题