You create images in the Tag Image File Format (TIFF). You want to add custom metadata to the image such as the location or other context information important for post-processing. How can you accomplish this?
- Install and import the library
- Use the
tiffile.imsave()function to store the file at a given location.
- As arguments, use the filename as the first positional argument, the image as the second positional argument.
- Then add your custom metadata as a string for the keyword argument
- You can now retrieve the metadata by calling the one-liner
Here’s an example that is a bit more readable:
import json import numpy as np import tifffile image = np.random.randint(0, 255, size=(100, 100), dtype=np.uint8) filename = 'your_file.tif' # Create custom description my_description = "I recorded this image on Mars" # Write the file tifffile.imsave( filename, image, description = my_description ) # Read the file frames = tifffile.TiffFile(filename) page = frames.pages # Print file description print(page.tags["ImageDescription"].value)
You can try this example in our interactive Jupyter Notebook in your browser to test if this is what you need:
I hope you liked this short tutorial! If you want to boost your Python skills on autopilot, check out my free email academy:
We have cheat sheets! 😉
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.