DEV Community

Sam Desmond Schulman
Sam Desmond Schulman

Posted on Edited on AI-assisted

Read the TIFF metadata before you measure anything

I wrote this post with help from an AI writing tool.

An easy mistake in microscopy image analysis is trusting the file name. The information that matters for a measurement (pixel size, bit depth, exposure, channel, acquisition date) lives in the TIFF metadata, not the name.

Here is a small Python routine for checking a folder of TIFFs. It uses the tifffile library, which handles plain TIFF, OME-TIFF (a TIFF that stores standard microscopy metadata as XML) and most microscope vendor variants.

Install

pip install tifffile
Enter fullscreen mode Exit fullscreen mode

Read one file

import tifffile

path = "image_001.tif"
with tifffile.TiffFile(path) as tif:
    page = tif.pages[0]
    print("shape:", page.shape)
    print("dtype:", page.dtype)
    for tag in page.tags.values():
        print(tag.name, "=", tag.value)
Enter fullscreen mode Exit fullscreen mode

page.shape gives the pixel dimensions and page.dtype gives the bit depth. The difference between uint8 (whole numbers 0 to 255) and uint16 (0 to 65,535) matters a lot for intensity measurements. The loop prints every TIFF tag. The ones to look at first are XResolution, YResolution, ResolutionUnit and ImageDescription, because the pixel-to-micron calibration is usually in one of them.

OME-TIFF

If the file is OME-TIFF, the metadata is an XML block you can read directly:

with tifffile.TiffFile(path) as tif:
    if tif.is_ome:
        print(tif.ome_metadata)
Enter fullscreen mode Exit fullscreen mode

Check a whole folder

from pathlib import Path
import tifffile

folder = Path("images")
for path in sorted(folder.glob("*.tif")):
    with tifffile.TiffFile(path) as tif:
        page = tif.pages[0]
        desc = page.tags.get("ImageDescription")
        print(path.name, page.shape, page.dtype, (desc.value[:60] if desc else ""))
Enter fullscreen mode Exit fullscreen mode

Run this once per experiment. If two images in the same set have different shapes, bit depths or calibration, you find out before you build a pipeline on them, not after.

Why it matters

Intensity-based measurements assume every image was acquired the same way. The metadata is the only record of whether that is true, so check it first.

More about my work: samdesmondschulman.com

Thanks for reading,

Sam

Top comments (0)