Reading tiff image metadata in Python

Filipe Vargas picture Filipe Vargas · Sep 28, 2017 · Viewed 14.8k times · Source

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?

Answer

Martin picture Martin · Oct 24, 2017
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.