在Python中读取TIFF图像元数据

Fil*_*gas 6 python tiff python-imaging-library

如何从Python中的TIFF图像读取元数据,例如坐标?我foo._getexif()从PIL 尝试过,但收到消息:

AttributeError:“ TiffImageFile”对象没有属性“ _getexif”

是否可以通过PIL获得它?

Mar*_*tin 6

from PIL import Image
from PIL.TiffTags import TAGS

with Image.open('image.tif') as img:
    meta_dict = {TAGS[key] : img.tag[key] for key in img.tag.iterkeys()}
Run Code Online (Sandbox Code Playgroud)

_getexif()仅用于JPEG。JPEG需要解压缩元数据,而TIFF则不需要。也就是说,PIL不会天真地读取Exif标记或目录(不太直接)TIFF元数据。

  • iterkeys() 确实会产生错误,但 img.tag.keys() 对我有用。 (16认同)

小智 5

ExifRead将为您提供所需的技巧。尝试:

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

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

# Print the tag/ value pairs
for tag in tags.keys():
    if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
        print "Key: %s, value %s" % (tag, tags[tag])
Run Code Online (Sandbox Code Playgroud)


小智 5

由于第一个答案对我不起作用,我做了以下调整:

from PIL import Image
from PIL.TiffTags import TAGS

img = Image.open('test.tif')
meta_dict = {TAGS[key] : img.tag[key] for key in img.tag_v2}
Run Code Online (Sandbox Code Playgroud)

以下是我发现有用的一些链接:

https://pillow.readthedocs.io/en/stable/_modules/PIL/TiffTags.html https://hhsprings.bitbucket.io/docs/programming/examples/python/PIL/ExifTags.html https://github。 com/python-pillow/Pillow/issues/4940