# The problem of itk.ImageFileReader

**URL:** <https://discourse.slicer.org/t/the-problem-of-itk-imagefilereader/20798>\
**Category:** Development\
**Tags:** python\
**Created:** [November 26, 2021, 10:23am UTC](https://discourse.slicer.org/t/the-problem-of-itk-imagefilereader/20798 "2021-11-26T10:23:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![476663616](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/476663616/32/12994_2.png) [@476663616](https://discourse.slicer.org/u/476663616)\
**Post date:** [November 26, 2021, 10:23am UTC](https://discourse.slicer.org/t/the-problem-of-itk-imagefilereader/20798/1 "2021-11-26T10:23:22Z")

</div>

Hello,

I have encountered some problems recently. I want to rebulid the volume image from a “.tre” file to show the refine image. I use itk to complete this work, and it works perfect in the conda env. However, when I run it in my Slicer extension, it shows:

```auto
TemplateImageType: <class 'itk.itkImagePython.itkImageF3'>
terminate called after throwing an instance of 'H5::DataSpaceIException'
error: [/mnt2/homes/xingyul/xinyul/Slicer/bin/SlicerApp-real] exit abnormally - Report the problem.

```

I tried to debug, found that this line of code caused the program to crash：

```auto
    TemplateImageType = itk.Image[itk.F, 3]
    print('TemplateImageType:',TemplateImageType)
    TemplateImageReaderType = itk.ImageFileReader[TemplateImageType]

```

I think the function ‘itk.ImageFileReader’ caused the conflict, how to fix it?

Thanks

---

<div class="post-metadata">

**Author:** ![476663616](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/476663616/32/12994_2.png) [@476663616](https://discourse.slicer.org/u/476663616)\
**Post date:** [November 26, 2021, 10:24am UTC](https://discourse.slicer.org/t/the-problem-of-itk-imagefilereader/20798/2 "2021-11-26T10:24:42Z")

</div>

The entire code is as follows：

```auto
import itk
from itk import TubeTK as ttk
def treTomask(rootpath, ctpath):
    ctpath=''
    print('start treTomask')
    PixelType = itk.F
    Dimension = 3
    ImageType = itk.Image[PixelType, Dimension]
        
    # # Read tre file
    TubeFileReaderType = itk.SpatialObjectReader[Dimension]
        
    tubeFileReader = TubeFileReaderType.New()
    tubeFileReader.SetFileName(rootpath)
    tubeFileReader.Update()

    print('tubeFileReader complete')

    tubes = tubeFileReader.GetGroup()

    print('tubes = tubeFileReader.GetGroup()')

    # Read template image
    TemplateImageType = itk.Image[itk.F, 3]
    print('TemplateImageType:',TemplateImageType)
    TemplateImageReaderType = itk.ImageFileReader[TemplateImageType]
    print('TemplateImageReaderType')
    print(ctpath)
        
    templateImageReader = TemplateImageReaderType.New()
    templateImageReader.SetFileName(ctpath)
    templateImageReader.Update()
    print('templateImageReader')

    templateImage = templateImageReader.GetOutput()
    TubesToImageFilterType = ttk.ConvertTubesToImage[TemplateImageType]
    tubesToImageFilter = TubesToImageFilterType.New()
    tubesToImageFilter.SetUseRadius(True)
    tubesToImageFilter.SetTemplateImage(templateImageReader.GetOutput())
    tubesToImageFilter.SetInput(tubes)
    tubesToImageFilter.Update()
    outputImage = tubesToImageFilter.GetOutput()

    print('tubesToImageFilter complete')

    TTKImageMathType = ttk.ImageMath[ImageType,ImageType]
    imMath = TTKImageMathType.New(Input = outputImage)
    img = imMath.GetOutput()
    # os.remove(rootpath)
    print('Tre convert is DONE')
    return img

```

---

<div class="post-metadata">

**Author:** ![476663616](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/476663616/32/12994_2.png) [@476663616](https://discourse.slicer.org/u/476663616)\
**Post date:** [November 26, 2021, 12:37pm UTC](https://discourse.slicer.org/t/the-problem-of-itk-imagefilereader/20798/3 "2021-11-26T12:37:36Z")

</div>

By the way, I try subprocess, like this:

```auto
subprocess.run('/mnt2/homes/xingyul/anaconda3/envs/labelsys/bin/python /mnt2/homes/xingyul/xinyul/Slicer/vessel_connect/infer_tools_tree/result/test.py', shell=True)

```

but it seems doesn’t work ：

```auto
Traceback (most recent call last):
  File "/mnt2/homes/xingyul/xinyul/Slicer/vessel_connect/infer_tools_tree/result/test.py", line 1, in <module>
    from TreToImg import treTomask
  File "/mnt2/homes/xingyul/xinyul/Slicer/vessel_connect/infer_tools_tree/result/TreToImg.py", line 1, in <module>
    import itk
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/itk/ __init__.py", line 28, in <module>
    from itk.support.extras import *
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/itk/support/extras.py", line 23, in <module>
    import numpy as np
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/numpy/ __init__.py", line 140, in <module>
    from . import core
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/numpy/core/ __init__.py", line 22, in <module>
    from . import multiarray
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/numpy/core/multiarray.py", line 12, in <module>

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [November 26, 2021, 2:02pm UTC](https://discourse.slicer.org/t/the-problem-of-itk-imagefilereader/20798/4 "2021-11-26T14:02:41Z")

</div>

This thread should help: [Slicer crashes when creating h5 file - #3 by szymswiat](https://discourse.slicer.org/t/slicer-crashes-when-creating-h5-file/19733/3)

---

<div class="post-metadata">

**Author:** ![476663616](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/476663616/32/12994_2.png) [@476663616](https://discourse.slicer.org/u/476663616)\
**Post date:** [November 27, 2021, 12:42pm UTC](https://discourse.slicer.org/t/the-problem-of-itk-imagefilereader/20798/5 "2021-11-27T12:42:41Z")

</div>

Thanks for reply！I have successfully fixed it by subprocess, but I’d like to try your suggestion.
