Generating python template parameter from an unknown image

Hi,

I’m looking for a simple solution to the following:

I’d like to generate an image type template parameter to use in order to construct an ImageSeriesReader class (python) by interrogating one of the files in the series first and extracting the component type (PixelType) without reading it fully first.The component type in this file is not known in advance but will be one of the types I’ve provided python wrappings for.

I’ve seen some examples that partially show how I can do this via the use of ReadImageInformation() from an ImageIO object created from a given file, https://discourse.itk.org/t/error-while-reading-an-image-with-itk-python/1560/2

Also, I’ve seen the solution given in ReadUnknownImageType

However, I can’t see a simple method that maps the value or string returned by GetComponentType() or GetComponentTypeAsString() that can be utilised to construct the tuple for the template parameter. Ie, in itk.ctype() etc, without writing a whole lot of manual matching code as per the second example.

I know I can get around this by fully reading the image first and extracting the template directly with itk.template(image), however in my particular case I’d like to avoid this due to the extremely large size of the images involved.

Does anyone know of a better way of achieving this?

Regards,

Darren

Hi Darren,

This capability, to quickly read a series of TIFF, DICOM, or PNG files, for example, into a 3D volume, is already available in the itk 5.0 Python wrapping with itk.imread! :sunny:

$ pip install --upgrade itk

Then,

import itk
import glob

files = glob.glob('my/directory/*.tif')
files.sort()
volume = itk.imread(files)

The relevant implementation, if you are still interested, is here:

Thanks Matt,

That’s pretty much the info I was looking for!

Darren

1 Like