在python中读取长exif标签

时间:2014-01-02 16:15:23

标签: python python-imaging-library exif exiftool

我正在尝试读取像这样的显微镜图像的EXIF信息: https://dl.dropboxusercontent.com/u/3816350/E3-9.tif

我对“图像描述”标签最感兴趣,因为它包含有关图像比例的信息。我已经使用exifread包成功加载了EXIF信息:

import exifread

f = open('E3-9.tif', 'rb')
exif_info = exifread.process_file(f)

for tag in exif_info.keys():
    print "Key: %s, value %s" % (tag, exif_info[tag])

但是,图像描述在输出中被截断,我无法弄清楚如何显示整个“Image ImageDescription”字段。知道我怎么能这样做吗?

BTW,我尝试使用PIL使用以下代码读取EXIF信息(如here所述):

from PIL import Image
from PIL.ExifTags import TAGS

img = Image.open('E3-9.tif')
exif_data = img._getexif()

但是我收到以下错误:

Traceback (most recent call last):
  File "/Users/..../2014-01-02 - Read scale from tif file.py", line 22, in <module>
    exif_data = img._getexif()
  File "/Users/danhickstein/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/PIL/Image.py", line 512, in __getattr__
    raise AttributeError(name)
AttributeError: _getexif

我也在命令行上尝试了exiftool,但它也略微切断了Image Description字段。

任何提示都将不胜感激。

3 个答案:

答案 0 :(得分:0)

您正在查找的元数据可能是图片的IPTC元数据的一部分,而不是EXIF。如果是这样,您将需要一个不同的Python模块来阅读它。请查看"Exif manipulation library for python [closed]"以获取包含IPTC数据的建议。

答案 1 :(得分:0)

这是使用subprocess.check_output在命令行上调用exiftool的非常缓慢且低效的方法。不是我最好的时刻,但它确实有效:

import matplotlib.pyplot as plt
import subprocess, glob, re

def get_magnification(filename):
    p = subprocess.check_output('exiftool -tab %s'%filename,shell=True) 

    xpix = float(re.findall('XpixCal=\d*.\d*',p)[0][8:])
    ypix = float(re.findall('YpixCal=\d*.\d*',p)[0][8:])

    mag = int(re.findall('p.\d+',p)[0][2:])

    return xpix,ypix,mag

xpix,ypix,mag = get_magnification('E3-9.tif')

print 'X pixels per nm: %.3f'%(xpix)
print 'Y pixels per nm: %.3f'%(ypix)
print 'Magnification:   %ix'%(mag)

答案 2 :(得分:0)

您必须使用exif_info [tag] .values来获取完整的ImageDescription