繁体   English   中英

在 Python 中读取 tiff 图像元数据

[英]Reading tiff image metadata in Python

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

AttributeError: 'TiffImageFile' 对象没有属性 '_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-枕头/枕头/问题/4940

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM