2015-01-13 105 views
0

我想不通爲什麼_getexif()返回一個空字典。我知道,圖像包含EXIF數據如圖ImageMagic:EXIF python PIL返回空字典

>> identify -verbose image.jpg 

Properties: 
    date:create: 2015-01-12T16:20:26-05:00 
    date:modify: 2013-07-01T14:05:08-04:00 
    exif:ColorSpace: 1 
    ... 

exif:Model: PC800 PROFESSIONAL 
    exif:ResolutionUnit: 2 
    exif:SceneCaptureType: 0 
    exif:WhiteBalance: 1 
    exif:XResolution: 72/1 
    exif:YCbCrPositioning: 2 
    exif:YResolution: 72/1 
    jpeg:colorspace: 2 
    jpeg:sampling-factor: 2x1,1x1,1x1 
    signature: e63fcbacdfd031e611b83befaa4a9c8ef6235894da10784a692f90832205ec60 
    Profiles: 
    Profile-exif: 931 bytes 

現在嘗試與python2讀取EXIF

img = Image.open('image.jpg') 
>>> img 
<PIL.JpegImagePlugin.JpegImageFile image.jpg mode=RGB size=2048x1536 at 0x7FEDF2E8A518> 
exif = img._getexif() 
>>> exif 

返回任何...

我也試圖與pyexiv2,同結果:

>>> import pyexiv2 
>>> metadata = pyexiv2.ImageMetadata(img) 
>>> metadata.read() 
>>> metadata.exif_keys 
[] 

任何想法,爲什麼我不能將這些exif數據讀入python? Arch Linux上的Python 2.7.8(默認,2014年9月24日,18:26:21)。

+0

PIL適合我。你在這裏使用示例圖像表單http://www.exiv2.org/sample.html –

+0

在PIL的這個圖像示例中工作得很好。所以Python不能讀取我的圖像元數據,但ImageMagic可以。 Python和Reconyx相機元數據的任何已知問題? – Chargaff

+0

我想我可以嘗試通過imagemagick調用從python讀取元數據? – Chargaff

回答

1

作爲替代使用_getexif()我們可以使用subprocess.chck_output運行identify和解析輸出到字典:

from subprocess import check_output 

c = check_output("identify -verbose img.jpg".split()) 
d = {} 
print(c) 
for line in c.splitlines()[:-1]: 
    spl = line.split(":",1) 
    k, v = spl 
    d[k.lstrip()] = v.strip() 
print(d) 

{'Blue': '', 'Border color': 'srgb(223,223,223)', 'Compression': 'JPEG', 'Elapsed time': '0:01.000', 'Artifacts': '', 'Overall': '', 'blue primary': '(0.15,0.06)', 'Dispose': 'Undefined', 'jpeg': 'sampling-factor: 2x2,1x1,1x1', 'red primary': '(0.64,0.33)', 'Interlace': 'JPEG', 'Compose': 'Over', 'Tainted': 'False', 'Filesize': '3.9KB', 'Pixels per second': '0B', 'Units': 'Undefined', 'Chromaticity': '', 'Number pixels': '7.5K', 'Type': 'TrueColor', 'Red': '', 'Channel depth': '', 'blue': '8-bit', 'Background color': 'white', 'Transparent color': 'black', 'verbose': 'true', 'min': '0 (0)', 'Resolution': '72x72', 'Image statistics': '', 'filename': 'img.jpg', 'green primary': '(0.3,0.6)', 'Print size': '1.38889x1.04167', 'Channel statistics': '', 'Class': 'DirectClass', 'red': '8-bit', 'Matte color': 'grey74', 'Page geometry': '100x75+0+0', 'skewness': '-0.284137', 'Geometry': '100x75+0+0', 'max': '255 (1)', 'Colorspace': 'sRGB', 'Depth': '8-bit', 'Green': '', 'date': 'modify: 2015-01-13T19:18:06+00:00', 'User time': '0.000u', 'Rendering intent': 'Perceptual', 'kurtosis': '0.369947', 'Gamma': '0.454545', 'Orientation': 'Undefined', 'Endianess': 'Undefined', 'Image': 'img.jpg', 'Format': 'JPEG (Joint Photographic Experts Group JFIF format)', 'white point': '(0.3127,0.329)', 'Properties': '', 'green': '8-bit', 'Iterations': '0', 'signature': '620e0541b826edb6dc5a2420beaaf697fdf6682ba50f915b8ab767fafbb3b7d2', 'standard deviation': '46.9874 (0.184264)', 'Quality': '85', 'mean': '133.602 (0.52393)'} 

如果你不想與空值的鍵使用if v之前添加鍵/值