在Python中读取TIFF图像元数据

问题描述:

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

How can I read metada, like coordinates, from a TIFF image in Python? I tried foo._getexif() from PIL, but got the message:

AttributeError:"TiffImageFile"对象没有属性"_getexif"

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

是否可以通过PIL获得它?

Is it possible to get it with 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元数据.

_getexif() is only meant to be used with JPEG. JPEG requires unpacking of the metadata, TIFF does not. That said, PIL does not naively read Exif tags or directory (less straightforward) TIFF metadata.