How to display specific IFD of a TIFF file using Python

632 Views Asked by At

I have a tiff file with several Image File Directories (IFD). I would like to use Python to specifically display the image associated with an IFD.

I know I can use tools such as tiffdump of tiffinfo to display all the relevant metadata for each IFD. I also know that I can view these images in readers such as ImageJ, QuPath, OpenSlide. But, to my knowledge, none of these tools allow me to specifically display the image of a given IFD. Or can they?

In summary, I need a tool that allows me to display the image associated with a given IFD, regardless if the image is one of the special ones or the reserved ones.

Context: In my work, I come across many tiff files which are composed of multiple IFDs (Image File Directories). These IFDs typically store different resolutions of the baseline image which, despite increasing the file size, allow readers to display images very fast. Typically some of the IFDs are also reserved for special images such as a macro or an image of a label associated with an image.

1

There are 1 best solutions below

1
Mark Setchell On

Not really sure what you are looking for, but I downloaded CMU-1-Small-Region.svs.tiff from your link and can do the following...

I can see what IFDs are in there and their sizes with ImageMagick as follows - showing 4 IFDs in this instance (one per line):

magick identify CMU-1-Small-Region.svs.tiff

CMU-1-Small-Region.svs.tiff[0] TIFF 2220x2967 2220x2967+0+0 8-bit sRGB 1.84913MiB 0.000u 0:00.000
CMU-1-Small-Region.svs.tiff[1] TIFF 574x768 574x768+0+0 8-bit sRGB 0.000u 0:00.000
CMU-1-Small-Region.svs.tiff[2] TIFF 387x463 387x463+0+0 8-bit sRGB 0.000u 0:00.000
CMU-1-Small-Region.svs.tiff[3] TIFF 1280x431 1280x431+0+0 8-bit sRGB 0.000u 0:00.000

I can extract any of the 4 IFDs into its own TIFF file like this:

magick "CMU-1-Small-Region.svs.tiff[0]" IFD0.tiff
magick "CMU-1-Small-Region.svs.tiff[1]" IFD1.tiff

I can view any of the 4 IFDs like this:

magick "CMU-1-Small-Region.svs.tiff[2]" display:
magick "CMU-1-Small-Region.svs.tiff[3]" display:  

If you don't like the ImageMagick X11 viewer (i.e. display:), you can extract any IFD and pipe it to a different viewer (e.g. feh) as a JPEG (or TIFF or PNG) like this:

magick "CMU-1-Small-Region.svs.tiff[0]" JPG:- | feh -

I can load the whole thing into Python Imaging Library like this, and seek to any IFD and display it or extract it:

from PIL import Image

# Load image
im = Image.open('CMU-1-Small-Region.svs.tiff')

# Seek to second IFD
im.seek(1)

# Display image
im.show()

# Save image
im.save('IFD-1.tiff')