读取 Python 中的 tiff 图像元数据

Reading tiff image metadata in Python

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

AttributeError: 'TiffImageFile' object has no attribute '_getexif'

是否可以通过PIL获取?

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()}

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

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])

由于第一个答案不适合我,我做了以下调整:

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}

以下是一些我认为有用的链接:

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