# Converting .nrrd file to .stl (using python)

**URL:** <https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166>\
**Category:** Support\
**Tags:** python\
**Created:** [July 3, 2022, 10:22pm UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166 "2022-07-03T22:22:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbaheti](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rbaheti](https://discourse.slicer.org/u/rbaheti)\
**Post date:** [July 3, 2022, 10:22pm UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/1 "2022-07-03T22:22:43Z")

</div>

Is there a way to convert .nrrd file to a .stl file on python?  
The nrrd file is binary (every voxel has a value of either 0 or 255).

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [July 4, 2022, 12:31am UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/2 "2022-07-04T00:31:33Z")

</div>

You should be able to do it using some kind of marching cubes filter

---

<div class="post-metadata">

**Author:** ![BrentFoster](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brentfoster/32/15062_2.png) [@BrentFoster](https://discourse.slicer.org/u/BrentFoster)\
**Post date:** [July 5, 2022, 3:09pm UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/3 "2022-07-05T15:09:38Z")

</div>

Hi Ron

I think the below script should work for this, but let me know if you have any questions on it.

import vtk  
label = 255  
nrrd\_file\_path = “”  
output\_stl\_path = “”

reader = vtk.vtkNrrdReader()  
reader.SetFileName(nrrd\_file\_path)  
reader.Update()  
img = read.GetOutput()

marching\_cubes\_filter = vtk.vtkDiscreteMarchingCubes()  
marching\_cubes\_filter.SetInputData(img)  
marching\_cubes\_filter.SetValue(0, 255)  
marching\_cubes\_filter.Update()  
polydata = marching\_cubes\_filter.GetOutput()

writer = vtk.vtkSTLWriter()  
writer.SetInputData(polydata)  
writer.SetFileName(output\_stl\_path)  
writer.Write()

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 5, 2022, 4:06pm UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/4 "2022-07-05T16:06:51Z")

</div>

If you want to see correctly reconstructed (smooth) surface then you can run these commands in Slicer’s Python environment:

```python
segmentation = slicer.util.loadSegmentation("/path/to/labelmap.nrrd")
slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles("/path/to/folder", segmentation)

```

See more useful code snippets for loading, processing, saving data in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.htm).

---

<div class="post-metadata">

**Author:** ![rbaheti](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rbaheti](https://discourse.slicer.org/u/rbaheti)\
**Post date:** [July 5, 2022, 10:41pm UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/5 "2022-07-05T22:41:24Z")

</div>

@mau_igna_06  
Thanks for the suggestion. I referred this [link](https://kitware.github.io/vtk-examples/site/Python/VisualizationAlgorithms/HeadBone/) to see how to use VTK to convert a file to stl format. I made the following changes to the code to suit my needs:

```auto
def get_program_parameters():
    parser.add_argument('filename', help='Trial.stl.')

def main():
    reader.SetFileName("trial_file.seg.nrrd")

```

The code, however, gives parsing error. I don’t understand the nature of this error:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/c/fc68b9a5634d8645a58ec4b27a9a9b09d9ec64ca.png)

---

<div class="post-metadata">

**Author:** ![rbaheti](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rbaheti](https://discourse.slicer.org/u/rbaheti)\
**Post date:** [July 5, 2022, 10:44pm UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/6 "2022-07-05T22:44:07Z")

</div>

@BrentFoster  
Thanks for the suggestion. This was a very straightforward approach [changed read.GetOutput() to reader.GetOutput()]!  
It created an STL file but the file is “malformed” and fails to open (the file size is 1KB).

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/4/f483eeaeb500ac0cf210e2a8c432a338c58aad12.png)

What exactly is going wrong?

---

<div class="post-metadata">

**Author:** ![rbaheti](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rbaheti](https://discourse.slicer.org/u/rbaheti)\
**Post date:** [July 5, 2022, 10:46pm UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/7 "2022-07-05T22:46:21Z")

</div>

@lassoan  
Thank you for the suggestion. Just as BrentFoster’s suggestion, the code is straightforward and doesn’t give any error while execution but no file is created. This is the output on the Python interface.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/f/1f397282c272ec00fef1b65fa20d2b4ad1679a64.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 6, 2022, 12:40am UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/8 "2022-07-06T00:40:11Z")

</div>

The output path must be a folder, not a filename, because this method exports all the segments of the segmentation, each as a separate file in the specified folder.

---

<div class="post-metadata">

**Author:** ![rbaheti](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rbaheti](https://discourse.slicer.org/u/rbaheti)\
**Post date:** [July 6, 2022, 12:53am UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/9 "2022-07-06T00:53:01Z")

</div>

@lassoan thanks for the reply!

To streamline the method I’m implementing is there a way to execute these commands on Spyder or Jupyter Notebook?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 6, 2022, 1:05am UTC](https://discourse.slicer.org/t/converting-nrrd-file-to-stl-using-python/24166/10 "2022-07-06T01:05:06Z")

</div>

Yes, sure, you can [use Slicer as a Jupyter Notebook kernel](https://github.com/Slicer/SlicerJupyter).
