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' object has no attribute '_getexif'
Is it possible to get it with PIL?
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' object has no attribute '_getexif'
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() 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.
Since the first answer didn't work for me, I made the following adjustment:
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}
Here are some links that I found useful:
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