在Python中读取tiff图像元数据

时间:2017-09-28 20:24:23

标签: python python-imaging-library tiff

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

  

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

是否可以通过PIL获取它?

3 个答案:

答案 0 :(得分:3)

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元数据。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

相关问题