# Wrap ITK Python

**URL:** <https://discourse.itk.org/t/wrap-itk-python/2391>\
**Category:** Algorithms\
**Created:** [November 8, 2019, 1:36pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391 "2019-11-08T13:36:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [November 8, 2019, 1:36pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391/1 "2019-11-08T13:36:15Z")

</div>

I’m trying to convert a C++ ITK code in python, but i find some issues with this input: TemplateTypeError: itk.ImageFileReader is not wrapped for input type `itk.Image[itk.UL,3]`. But i don’t know hot to solve the problem because i use this function to enter a .mha file.  
Code:

> import itk  
> import sys  
> import os  
> path=input(“Enter the path”)  
> Dim=3  
> PixelType=itk.F  
> LabelPixelType=itk.UL  
> ImageType=itk.Image[PixelType,Dim]  
> LabeledImageType=itk.Image[LabelPixelType,Dim]  
> MaskPixelType=itk.UC  
> MaskImageType=itk.Image[MaskPixelType,Dim]  
> #read the 3mm input volume  
> LabelReaderType=itk.ImageFileReader[LabeledImageType]  
> labelReader=LabelReaderType.New()  
> labelReader.SetFileName(path+“/T2.mha”)

Entire C++ code:  
[pkdMaskCreation.cxx](https://discourse.itk.org/uploads/short-url/xIrbMR5YjtfhTvkkUrWcm8u9Zc8.cxx) (4.8 KB)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 8, 2019, 2:24pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391/2 "2019-11-08T14:24:12Z")

</div>

I guess `itk::Image` is not instantiated with `unsigned long` by default, so that is not part of wheel distribution. Try `LabelPixelType=itk.US`. Alternatively [build ITK with Python wrapping](https://itk.org/Wiki/ITK/Python_Wrapping) yourself, and enable `unsigned long` for the wrapping.

---

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [November 8, 2019, 2:35pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391/3 "2019-11-08T14:35:59Z")

</div>

Thank you so much, with `LabelPixelType=itk.US` it seems to work. But i have another problem: if i want to use an iterator like `iteratorType=itk.ImageRegionIterator[LabeledImageType]` in python what i have to do, due to `AttributeError: module 'itk' has no attribute 'ImageRegionIterator'`. I read something about numpy, do you have some examples?

> typedef itk::ImageRegionIterator IteratorType;  
> IteratorType maskIt(outputImage,outputImage-\>GetLargestPossibleRegion());  
> LabelPixelType backgroundValue = 0;  
> LabelPixelType foregroundValue = 1;  
> for (maskIt.GoToBegin(); !maskIt.IsAtEnd(); ++maskIt)  
> {  
> ImageType::PointType point;  
> outputImage-\>TransformIndexToPhysicalPoint(maskIt.GetIndex(),point);  
> if (maskInterpolator-\>IsInsideBuffer(point))  
> {  
> if (maskInterpolator-\>Evaluate(point) == 0)  
> {  
> maskIt.Set(backgroundValue);  
> }  
> else  
> {  
> maskIt.Set(foregroundValue);  
> }  
> }  
> else  
> {  
> maskIt.Set(backgroundValue);  
> }  
> }

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 8, 2019, 2:42pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391/4 "2019-11-08T14:42:43Z")

</div>

`ImageRegionIterator` is not wrapped on purpose, because accessing images via iterators from Python is way too slow. Here is how to [convert to/from numpy](https://blog.kitware.com/convert-itk-data-structures-to-numpy-arrays/).

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [November 8, 2019, 2:49pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391/5 "2019-11-08T14:49:30Z")

</div>

If you want to expose an existing C++ ITK filter to python, wrap the whole filter to python, not the internals. This is how is done in all the external modules, [https://github.com/InsightSoftwareConsortium/ITKModuleTemplate](https://github.com/InsightSoftwareConsortium/ITKModuleTemplate)

You are trying to do in python what ITK does in c++, this is not performant. Better to compute it in c++ and then, expose it to python with the wrapping mechanisms of ITK. Especially if you already have the filter written in c++. Hope it helps!

---

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [November 8, 2019, 2:55pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391/6 "2019-11-08T14:55:40Z")

</div>

The problem is that the code is very older (many years ago) and some libraries in c++ changed,so some functions are no more supported, so i have also to modify the code

---

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [November 8, 2019, 3:30pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391/7 "2019-11-08T15:30:27Z")

</div>

Can i build a ITKModule with that procedure in Windows?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 8, 2019, 4:11pm UTC](https://discourse.itk.org/t/wrap-itk-python/2391/8 "2019-11-08T16:11:23Z")

</div>

Yes, Windows is one of the supported platforms.
