2015-08-19 47 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

相關問題