Python - Read TIFF stacks with 3 and 4 pages

Hi,

I’m trying to read tiff stacks with 3 or 4 images into itk with python like:

reader=itk.ImageFileReader.IF3.New(FileName=‘images.tif’)
image=itk.Image.F3.New()
reader.Update()
image=readerA.GetOutput()

Apparently, the stacks are interpreted as single RGB-Image and the image size is [x,y,1]. How is it possible to get the stack with 3 or 4 pages?

Thank you very much in advance!

Have you tried using imread?

You are specifying the type of image and number of dimensions of the output image as the “template” parameters to ImageFileReader. Some times you need to get the Image information from the ImageIO, then determine what the pixel type and dimension should be. The imread method should do some of that for you.

I am more familiar with SimpleITK so out of curiosity, have you tried reading the image with SimpleITK? Does it read it as expected? What type do you get?

If you can use the utility tiffinfo on the file to print the tags and dictionary that would be helpful to understand the structure of the file.

More details on itk.imread:

The image can be loaded with a default pixel type:

image = itk.imread('images.tif')

Check the type with:

print(type(image))

or, force an unsigned char pixel type:

image = itk.imread('images.tif', itk.UC)

or, force an RGB pixel type:

image = itk.imread('images.tif', itk.RGBPixel[itk.UC])

or, read in as an itk.VectorImage,

Dimension = 2
ImageType = itk.VectorImage[itk.UC,  Dimension]
reader = itk.ImageFileReader[ImageType].New(FileName='images.tif')
reader.Update()
image = reader.GetOutput()

Thanks alot! using

itk.imread('images.tif', itk.F)

works fine.

1 Like