ViewImage in Python

With the module ‘ITKVtkGlue’ wrapped the following script works to visualize images without the need for external software in python:

import itk
PixelType = itk.UC
Dimension = 3
ImageType = itk.Image[PixelType, Dimension]

imagePath = '/path/image3D.nrrd'
reader = itk.ImageFileReader.New(FileName=imagePath)
reader.Update()
itk.ViewImage[ImageType].View(reader.GetOutput())

Alternatively without declaring the ImageType beforehand:

import itk
imagePath = '/path/anyImage.nrrd'
reader = itk.ImageFileReader.New(FileName=imagePath)
reader.Update()
itk.ViewImage[type(reader.GetOutput())].View(reader.GetOutput())

Some samples:
2D

itk.ViewImage[type(reader.GetOutput())].View(reader.GetOutput(), '2DImage')


3D

itk.ViewImage[type(reader.GetOutput())].View(reader.GetOutput(), '3DImage')


ViewImage.View only requires passing the input image, optional arguments are ‘title’, windowWidth and windowHeight.

3 Likes