2011-01-21 92 views
68

我正在使用PIL。如何將EXIF數據轉換爲字典?在Python中,如何讀取圖像的exif數據?

+0

更近期的問題在這裏:http://stackoverflow.com/questions/14009148/exif-reading-library – 2013-04-24 12:58:59

+1

請參閱這裏的答案:http://stackoverflow.com/questions/765396/exif-manipulation-library-for-python – 2011-01-22 00:05:19

+0

http://tilloy.net/dev/pyexiv2/tutorial.html這是最簡單和最全面的。 – 2012-11-17 19:10:49

回答

108

試試這個:

import PIL.Image 
img = PIL.Image.open('img.jpg') 
exif_data = img._getexif() 

這應該給你通過EXIF數字標籤索引的字典。如果您想通過實際的EXIF標籤名稱字符串索引的字典中,你可以試試:

import PIL.ExifTags 
exif = { 
    PIL.ExifTags.TAGS[k]: v 
    for k, v in img._getexif().items() 
    if k in PIL.ExifTags.TAGS 
} 
+5

任何Python 3的替代? – 2013-08-30 04:57:31

20

您也可以使用ExifRead模塊:

import exifread 
# Open image file for reading (binary mode) 
f = open(path_name, 'rb') 

# Return Exif tags 
tags = exifread.process_file(f) 
11

我用這個:

import os,sys 
from PIL import Image 
from PIL.ExifTags import TAGS 

for (k,v) in Image.open(sys.argv[1])._getexif().iteritems(): 
     print '%s = %s' % (TAGS.get(k), v) 

或獲取特定字段:

def get_field (exif,field) : 
    for (k,v) in exif.iteritems(): 
    if TAGS.get(k) == field: 
     return v 

exif = image._getexif() 
print get_field(exif,'ExposureTime')