# How to convert stl to nii

**URL:** https://discourse.slicer.org/t/how-to-convert-stl-to-nii/37282
**Category:** Support
**Tags:** segmentation, python, segmentations, segment-editor
**Created:** [July 9, 2024, 2:03pm UTC](https://discourse.slicer.org/t/how-to-convert-stl-to-nii/37282 "2024-07-09T14:03:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pc666nice](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pc666nice/32/77252_2.png) [@pc666nice](https://discourse.slicer.org/u/pc666nice)
#### Post date: [July 9, 2024, 2:03pm UTC](https://discourse.slicer.org/t/how-to-convert-stl-to-nii/37282/1 "2024-07-09T14:03:35Z")

</div>

Dear all:  
I’m trying to convert stl file to nii for the medical image segmentation, however, when I use the method in the link :  
[[Converting .stl files to binary label maps in .nii format using Python - Support - 3D Slicer Community](https://discourse.slicer.org/t/converting-stl-files-to-binary-label-maps-in-nii-format-using-python/13038)](https://), but the generated mask didn’t match the original image very well.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/3/a3553160ec144b54ea0fbe14c9c50ea85b61cfb4.jpeg)

When I use the similar method in Mimics, the mask matched the image:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/0/f028b2ed2b023373e15f77e3f2af3d90bb1b5b9b.jpeg)

Here is the code that I use:

```auto

import os
import slicer

stl_file_name = "D:/PengChen/zhongshan/CFDPCNNmd/stls/1.stl"
output_file_name = "D:/PengChen/zhongshan/CFDPCNNmd/stl_niis/1.nii.gz"
reference_volume_path = "D:/PengChen/zhongshan/CFDPCNNmd/images/1.nii.gz"

referenceVolumeNode = slicer.util.loadVolume(reference_volume_path)
segmentationNode = slicer.util.loadSegmentation(stl_file_name) ## stl
outputLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, outputLabelmapVolumeNode, referenceVolumeNode)
slicer.util.saveNode(outputLabelmapVolumeNode, output_file_name)

```

Using the above code, I can generated the file but it only has the background, not the binary labelmap.

I’m looking forward to get the correct method, thanks a lot.

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [July 10, 2024, 9:12am UTC](https://discourse.slicer.org/t/how-to-convert-stl-to-nii/37282/2 "2024-07-10T09:12:50Z")

</div>

First of all we need to know where do you get the STL from. If you produce it in Slicer, the answer will be quite different from if you get it externally.

> [@pc666nice](#):
>
> didn’t match the original image very well

I assume you are talking about the voxelization error, meaning that the voxels are too large to fit well the smooth STL boundaries. Do I understand correctly?

---

<div class="post-metadata">

### Author: ![pc666nice](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pc666nice/32/77252_2.png) [@pc666nice](https://discourse.slicer.org/u/pc666nice)
#### Post date: [December 27, 2024, 8:57am UTC](https://discourse.slicer.org/t/how-to-convert-stl-to-nii/37282/3 "2024-12-27T08:57:47Z")

</div>

Thanks for your reply, that’s right, because there is something wrong when I transfer the dicom files to the nii.gz files
