Type annotations and itk.Image[...]

I am writing some Python code built on itk and other packages. Many of the methods take itk images as arguments.

What is the best way add type annotations for itk images? In most cases I don’t care about the pixel type of the image, but do care about the dimension.

  • Is there a corresponding base type in Python for 2D images, or 3D images?
  • Or should I use typing.Union and list all the image types I except to receive, like:
def do_something(img: Union[itk.Image[itk.UC,2], itk.Image[itk.F,2]], a:int, b:float):

@hjmjohnson or @matt.mccormick could best answer this.

I found ImageLike and ImageBase, i.e.
from itk.support.types import ImageLike, ImageBase

This seems to be more or less what I want, although the dimension is lost. I guess I could do a Union of images with all valid pixel types.

2 Likes

Hi!
I’m currently working on generating typehints for ITK’s Python wrapped classes and methods which should help new users understand the package quicker.

While there may not be exactly what you want there is a itkImageBase2 class that is inherited by all 2 dimensional itk Images in Python regardless of pixel type.

You could try the following:

from itk.itkImagePython import itkImageBase2
def do_something(img: itkImageBase2, a:int, b:float):
    ...

Hope this helps!

4 Likes

thanks. that helps