How to Get TIFF Tag Data with Python
Last week, you learned how to get EXIF metadata from your JPEG files. This week, you will learn how to get similar data from the TIFF image format.
Pillow provides a similar dictionary for TIFF images in its TiffTags module. If you need a TIFF image, you can use this one, which is a cover from one of the author's other books on ReportLab:
You can create your own TIFF metadata extractor utility by making a new file named tiff_metadata.py and adding this code to it:
Here, you import the TAGS dictionary from the PIL.TiffTags submodule. Then, in get_metadata(), you access the tag elements in the image by iterating over the contents of tag.items(). To make that information more readable, you use the TAGS dictionary that you imported.
Here is a sample of the output you will get when you run this code:
You can see that the value entries above are all tuples. This is because of how the data is returned from the tag data. If you would like a challenge, you can try cleaning up this data a bit in your version of the metadata extraction utility.
Wrapping Up
EXIF and TIFF metadata are really useful for encoding lots of information in your images. However, most people don’t even know that data is there! Knowing how to access your photo’s metadata allows you to do all kinds of programmatic tasks, such as resizing, sorting files by various parameters, and much more.
You can use Pillow and Python to do all kinds of other image processing, so this is just scratching the surface. Download Pillow today and start learning!
Want to Learn More?
You can learn more about what you can do with Python and Pillow in Mike’s book, Pillow: Image Processing with Python




