Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading tiff image metadata in Python

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?

like image 545
Filipe Vargas Avatar asked Sep 28 '17 20:09

Filipe Vargas


People also ask

Do TIFF files have metadata?

An image file - whether it's in JPEG, TIFF, PSD, Raw or several other formats - can include a range of metadata.


2 Answers

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.

like image 174
Martin Avatar answered Sep 21 '22 05:09

Martin


ExifRead will do the trick for what you want. Try:

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])
like image 28
Landini135 Avatar answered Sep 22 '22 05:09

Landini135