We previously announced that the ITK Python wrapping is now available in the Python Package Index (PyPI) .
Starting to develop Python scripts using ITK filters is one command line away:
python -m pip install itk
Python is a scripting language that allows fast development and is widely used in the scientific community.
You can use ITK Python to read and write all the common image formats as well as many others. Combined with the power and speed of the hundreds of ITK filters, it is fast and easy to create and adapt complex image processing pipelines. The example below illustrates how to create a simple pipeline that reads an image, processes it with a median filter, and saves the result.
import itk image = itk.imread("input_filename.png") median = itk.MedianImageFilter.New(image, Radius = 2) itk.imwrite(median, "output_filename.png")
Sometimes, such as when one wants to display an image in an IPython notebook, it is necessary to directly access the raw content of an image. It is possible through the ITK image object to get the image values pixel by pixel, but manually iterating over the entire image would result in slow processing. Instead, ITK provides a bridge to convert ITK data to NumPy arrays. The conversion can be performed in both directions. It is therefore possible to combine the speed and power of ITK to process images, and the speed and versatility of NumPy to accomplish specific operations that are not available in ITK.
The ITK NumPy bridge converts ITK images, but also vnl vectors and vnl matrices to NumPy arrays. The data can either be copied into a new object or a view on the data can be created. The view allows access and modification of the data without the need to duplicate its memory. Depending on the situation, a view or copy is preferred.
The functions to convert ITK images to/from NumPy arrays/views are:
image_view = itk.GetImageViewFromArray( np_array) image = itk.GetImageFromArray( np_array) array_view = itk.GetArrayViewFromImage(image) array = itk.GetArrayFromImage(image)
All the functions to convert to and from vnl vectors and vnl matrices are presented in the ITK Python Quick Start Guide  (Hint: It is easy to guess their names).
A short example illustrating how to use ITK Python and the ITK NumPy bridge is available here .
Have fun using ITK!