# itk.ImageToVTKImageFilter crashing Python

**URL:** https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018
**Category:** Algorithms
**Tags:** python
**Created:** [July 9, 2019, 2:03am UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018 "2019-07-09T02:03:50Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Michael\_Hogg](https://discourse.itk.org/user_avatar/discourse.itk.org/michael_hogg/32/851_2.png) [@Michael\_Hogg](https://discourse.itk.org/u/Michael_Hogg)
#### Post date: [July 9, 2019, 2:03am UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/1 "2019-07-09T02:03:50Z")

</div>

Hi,

I am using itk and vtk in Python. Specifically I am trying to convert an ITK image to a VTK image using the function itk.ImageToVTKImageFilter. I am currently trying to replicate the code [here](https://itk.org/ITKExamples/src/Bridge/VtkGlue/ConvertAnitkImageTovtkImageData/Documentation.html). If I even try to access this function (just to read the documentation!) then it causes Python to crash and exit.

Has anyone else tested this function and had problems? Should I file a bug report?

Versions:  
OS: Windows 7  
Python 3.6  
ITK v5.0.0.post1 from PyPi  
VTK v8.1.2 from PyPi

Cheers,  
Michael

---

<div class="post-metadata">

### Author: ![Michael\_Hogg](https://discourse.itk.org/user_avatar/discourse.itk.org/michael_hogg/32/851_2.png) [@Michael\_Hogg](https://discourse.itk.org/u/Michael_Hogg)
#### Post date: [July 9, 2019, 2:21am UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/2 "2019-07-09T02:21:20Z")

</div>

I tracked it down. The team is aware and will be fixed in the next release.

> <https://github.com/InsightSoftwareConsortium/ITKVtkGlue/issues/23>

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [July 22, 2019, 8:18pm UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/3 "2019-07-22T20:18:41Z")

</div>

@Michael_Hogg please try itk-vtkglue 0.3.0 and let us know how it goes.

---

<div class="post-metadata">

### Author: ![Michael\_Hogg](https://discourse.itk.org/user_avatar/discourse.itk.org/michael_hogg/32/851_2.png) [@Michael\_Hogg](https://discourse.itk.org/u/Michael_Hogg)
#### Post date: [July 22, 2019, 10:00pm UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/4 "2019-07-22T22:00:33Z")

</div>

Hi Matt,

Just tried itk-vtkglue 0.3.0 - works as expected. Thanks!

Michael

---

<div class="post-metadata">

### Author: ![Phong\_Tran](https://discourse.itk.org/user_avatar/discourse.itk.org/phong_tran/32/1024_2.png) [@Phong\_Tran](https://discourse.itk.org/u/Phong_Tran)
#### Post date: [October 8, 2019, 6:57am UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/5 "2019-10-08T06:57:49Z")

</div>

Hi Matt,  
I use the latest itk-vtkglue using this : python -m pip install itk-vtkglue on Ubuntu 18, but when I tried to run the example it coredumped. This is the code sample:  
import itk  
import sys

if len(sys.argv) != 2:  
print('Usage: ’ + sys.argv[0] + ’ ')  
sys.exit(1)  
imageFileName = sys.argv[1]

Dimension = 2  
PixelType = itk.UC  
ImageType = itk.Image[PixelType, Dimension]

reader = itk.ImageFileReader[ImageType].New()  
reader.SetFileName(imageFileName)

itkToVtkFilter = itk.ImageToVTKImageFilter[ImageType].New()  
itkToVtkFilter.SetInput(reader.GetOutput())

itkToVtkFilter.Update()  
myvtkImageData = itkToVtkFilter.GetOutput()  
print(myvtkImageData)

When running it:  
╭─phongtd@nguyenth ~/tmp/itk-vtk-glue/build  
╰─$ python …/itk2vtk.py ./out.nrrd 1 ↵  
[1] 16358 segmentation fault (core dumped) python …/itk2vtk.py ./out.nrrd

Any suggestion ?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [October 8, 2019, 10:01pm UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/6 "2019-10-08T22:01:46Z")

</div>

Hi Phong,

Welcome to the ITK Discourse! ☀

Thank you for the detailed, self-contained description of the issue. I was able to reproduce the segfault. I am building in _Debug_ to isolate the issue.

Matt

---

<div class="post-metadata">

### Author: ![Phong\_Tran](https://discourse.itk.org/user_avatar/discourse.itk.org/phong_tran/32/1024_2.png) [@Phong\_Tran](https://discourse.itk.org/u/Phong_Tran)
#### Post date: [October 19, 2019, 7:51am UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/7 "2019-10-19T07:51:52Z")

</div>

After several gooling and research on code, I was able to use itk.ImageToVTKImageFilter by using my own built ITK source code. For those whose are new comer like me, hope this help you. First of all, I compile source code of ITK release version ([ITK Release](https://github.com/InsightSoftwareConsortium/ITK/releases/download/v5.0.1/InsightToolkit-5.0.1.tar.gz)). Note that I tried the ITK Github ([ITK Github](https://github.com/InsightSoftwareConsortium/ITK)), but did not succeed. So I recommend you to compile with Release version. For VTK source code, I also used the the Release version 8.2, not the Github code. The next step I did is to enable the ITK VTK Glue in CMakeCache to ON,adn ITK\_WRAP\_PYTHON to ON. like this:

> ╭─phongtd@nguyenth ~/workingspace/InsightToolkit-5.0.1/build\_other  
> ╰─$ grep Module\_ITKVtkGlue CMakeCache.txt  
> **Module\_ITKVtkGlue:BOOL=ON**  
> //ADVANCED property for variable: Module\_ITKVtkGlue  
> Module\_ITKVtkGlue-ADVANCED:INTERNAL=1

> ╭─phongtd@nguyenth ~/workingspace/InsightToolkit-5.0.1/build\_other  
> ╰─$ grep PYTHON CMakeCache.txt  
> **ITK\_WRAP\_PYTHON:BOOL=ON**  
> ITK\_WRAP\_PYTHON\_LEGACY:BOOL=ON  
> PYTHON\_EXECUTABLE:FILEPATH=/usr/bin/python  
> PYTHON\_INCLUDE\_DIR:PATH=/usr/include/python3.6m  
> PYTHON\_LIBRARY:FILEPATH=/usr/lib/x86\_64-linux-gnu/libpython3.6m.so  
> PYTHON\_LIBRARY\_DEBUG:FILEPATH=PYTHON\_LIBRARY\_DEBUG-NOTFOUND  
> ITK\_WRAP\_PYTHON\_BINARY\_DIR:INTERNAL=/home/phongtd/workingspace/InsightToolkit-5.0.1/build\_other/Wrapping/Generators/Python  
> ITK\_WRAP\_PYTHON\_SOURCE\_DIR:INTERNAL=/home/phongtd/workingspace/InsightToolkit-5.0.1/Wrapping/Generators/Python  
> //ADVANCED property for variable: PYTHON\_EXECUTABLE  
> PYTHON\_EXECUTABLE-ADVANCED:INTERNAL=1  
> //ADVANCED property for variable: PYTHON\_INCLUDE\_DIR  
> PYTHON\_INCLUDE\_DIR-ADVANCED:INTERNAL=1  
> //ADVANCED property for variable: PYTHON\_LIBRARY  
> PYTHON\_LIBRARY-ADVANCED:INTERNAL=1  
> //ADVANCED property for variable: PYTHON\_LIBRARY\_DEBUG  
> PYTHON\_LIBRARY\_DEBUG-ADVANCED:INTERNAL=1

On this step, I encountered many issues which I asked on the thread [https://discourse.itk.org/t/itk-python-wrapping-error-void-value-not-ignored-as-it-ought-to-be/948/10](https://discourse.itk.org/t/itk-python-wrapping-error-void-value-not-ignored-as-it-ought-to-be/948/10). Do the same thing on that thead you can compile the ITK with Python enabled.

The last step is to run the example. Before doing that, I set the PYTHONPATH variable to point to my ITK build like this:

> export PYTHONPATH=/home/phongtd/workingspace/InsightToolkit-5.0.1/build\_other/Wrapping/Generators/Python:$PYTHONPATH

And finally, the work is done without any issues.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [October 21, 2019, 4:10pm UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/8 "2019-10-21T16:10:08Z")

</div>

@Phong_Tran thank you for sharing your experience!

---

<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 7, 2019, 9:07am UTC](https://discourse.itk.org/t/itk-imagetovtkimagefilter-crashing-python/2018/9 "2019-11-07T09:07:12Z")

</div>

Thanks @Phong_Tran!

> [@Phong\_Tran](#):
>
> On this step, I encountered many issues which I asked on the thread [ITK Python Wrapping error: void value not ignored as it ought to be - #10 by Phong\_Tran](https://discourse.itk.org/t/itk-python-wrapping-error-void-value-not-ignored-as-it-ought-to-be/948/10). Do the same thing on that thead you can compile the ITK with Python enabled.

Just for clarifying the content of that thread: there won’t be issues if you **first build `VTK`** and **then `ITK`** using the **same python** for both.
