Saturday, October 24, 2026
HomePythonLearn how to Get TIFF MetaData with Python

Learn how to Get TIFF MetaData with Python


In earlier articles on this web site, you discovered tips on how to extract EXIF information from JPG picture recordsdata. This week, you’ll discover ways to get comparable information from the TIFF picture format.

The TIFF format additionally has its metadata. Pillow offers an identical dictionary for TIFF photographs in its TiffTags module. In the event you want a TIFF picture, you should use this one, which is a canopy from one of many writer’s different books on ReportLab:

You’ll be able to create your personal TIFF metadata extractor utility by making a brand new file named tiff_metadata.py and including this code to it:

# tiff_metadata.py

from PIL import Picture
from PIL.TiffTags import TAGS


def get_metadata(image_file_path):
    picture = Picture.open(image_file_path)
    metadata = {}
    for tag in picture.tag.gadgets():
        metadata[TAGS.get(tag[0])] = tag[1]
    return metadata


if __name__ == "__main__":
    metadata = get_metadata("reportlab_cover.tiff")
    print(metadata)

Right here you import the TAGS dictionary from the PIL.TiffTags submodule. Then in get_metadata(), you entry the tag components within the picture by iterating over the contents of tag.gadgets(). To make that data extra readable, you employ the TAGS dictionary that you just imported.

Here’s a pattern of the output you’ll get whenever you run this code:

{'ImageWidth': (400,),
 'ImageLength': (562,),
 'BitsPerSample': (8, 8, 8),
 'Compression': (1,),
 'PhotometricInterpretation': (2,),
 'FillOrder': (1,),
 'StripOffsets': (82, 130882, 261682, 392482, 523282, 654082),
 'Orientation': (1,),
 'SampleFormat': (1, 1, 1),
 'SamplesPerPixel': (3,),
 'RowsPerStrip': (109,),
 'StripByteCounts': (130800, 130800, 130800, 130800, 130800, 20400),
 'XResolution': ((300, 1),),
 'YResolution': ((300, 1),),
 'PlanarConfiguration': (1,),
 'ResolutionUnit': (2,),
 'ExifIFD': (8,),
 'Software program': ('Pixelmator 3.9',),
 'DateTime': ('2020:10:27 12:10:37',),
}

You’ll be able to see that the worth entries above are all tuples. That is due to how the info is returned from the tag information. If you need a problem, you possibly can try to wash up this information a bit in your model of the metadata extraction utility.

You should utilize Pillow and Python to do all types of different picture processing, so that is simply scratching the floor. Obtain Pillow immediately and begin studying!

Buy at Gumroad, Leanpub, or Amazon

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments