Generating python template parameter from an unknown image


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,

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?



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


import itk
import glob

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

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

1 Like

Thanks Matt,

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


1 Like

Hello, if I want to write out the volume, what should I do?
I used itk.ImageFileWriter (code below):

imwrite = itk.ImageFileWriter[ImageType].New()

Error message: TypeError: in method 'itkImageFileWriterIF3_SetInput', argument 2 of type 'itkImageF3 const *'
Can you give me some help? Thank you so much

Have you tried itk.imwrite(volume, 'filename.nrrd')? What is the full error message?

1 Like

Thank you for your help

1 Like

I just ran into this myself:

TypeError: in method 'itkImageFileReaderVIF3_SetFileName', argument 2 of type 'std::string const &'
Additional information:
Wrong number or type of arguments for overloaded function 'itkImageFileReaderVIF3_SetFileName'.
  Possible C/C++ prototypes are:
    itkImageFileReaderVIF3::SetFileName(itkSimpleDataObjectDecoratorstring const *)
    itkImageFileReaderVIF3::SetFileName(std::string const &)

The problem was that I got input_filename from argparse: for i, input_filename in enumerate(args.input_image): which made it a list consisting of one item. If I change reader.SetFileName(input_filename) into reader.SetFileName(input_filename[0]), the error goes away.