# Nifti segmentation to dicom-seg in a batch with python

**URL:** <https://discourse.slicer.org/t/nifti-segmentation-to-dicom-seg-in-a-batch-with-python/32488>\
**Category:** Development\
**Created:** [October 30, 2023, 5:08pm UTC](https://discourse.slicer.org/t/nifti-segmentation-to-dicom-seg-in-a-batch-with-python/32488 "2023-10-30T17:08:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdvp](https://avatars.discourse-cdn.com/v4/letter/g/2bfe46/32.png) [@gdvp](https://discourse.slicer.org/u/gdvp)\
**Post date:** [October 30, 2023, 5:08pm UTC](https://discourse.slicer.org/t/nifti-segmentation-to-dicom-seg-in-a-batch-with-python/32488/1 "2023-10-30T17:08:45Z")

</div>

Hi,

I use 3D slicer for a while, but am rather new with the python usage in slicer.  
I have to convert a lot of nifti files to dicom-seg format, which is a cumbersome task, so I wanted to do that in a batch as the nifti files with every CT dicom are named the same.

I tried first to do it with one scan and used the following code:  
‘’’  
import slicer  
from DICOMLib import DICOMUtils  
import DICOMScalarVolumePlugin  
import DICOMSegmentationPlugin

segmentationNode = r’path\_to\_segmentation\_in\_nifti’  
dicomDataDir = r’path\_to\_CT\_in\_dicom’  
outputFolder = r’path\_to\_output\_folder’

loadedNodeIDs = # this list will contain the list of all loaded node IDs  
with DICOMUtils.TemporaryDICOMDatabase() as db:  
DICOMUtils.importDicom(dicomDataDir, db)  
patientUIDs = db.patients()  
for patientUID in patientUIDs:  
loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

# Associate segmentation node with a reference volume node

shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)  
referenceVolumeShItem = shNode.GetItemByDataNode(loadedNodeIDs)  
studyShItem = shNode.GetItemParent(referenceVolumeShItem)  
segmentationShItem = shNode.GetItemByDataNode(segmentationNode)  
shNode.SetItemParent(segmentationShItem, studyShItem)

# Export to DICOM

exporter = DICOMSegmentationPlugin.DICOMSegmentationPluginClass()  
exportables = exporter.examineForExport(segmentationShItem)  
for exp in exportables:  
exp.directory = outputFolder  
exporter.export(exportables)  
‘’’

But I get an error when trying to read in the referenceVolume:  
Traceback (most recent call last):  
File “”, line 1, in   
TypeError: GetItemByDataNode argument 1: method requires a VTK object

I’m not sure how to get this NodeIDs into a VTK object. Could anyone help me give the golden tip.

Thanks!

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [October 30, 2023, 10:04pm UTC](https://discourse.slicer.org/t/nifti-segmentation-to-dicom-seg-in-a-batch-with-python/32488/2 "2023-10-30T22:04:22Z")

</div>

I think the issue is likely that `loadedNodeIDs` is a list of ID strings of image volume nodes, whereas `GetItemByDataNode()` expects its input to be a node, rather than a node ID. If you change the erroring line to

```auto
# Get the image node by its ID string
referenceVolumeNode = slicer.mrmlScene.GetNodeByID(loadedNodeIDs[0])
# Get the subject hierarchy item id corresponding to the 
referenceVolumeShItem = shNode.GetItemByDataNode(referenceVolumeNode)

```

---

<div class="post-metadata">

**Author:** ![gdvp](https://avatars.discourse-cdn.com/v4/letter/g/2bfe46/32.png) [@gdvp](https://discourse.slicer.org/u/gdvp)\
**Post date:** [October 31, 2023, 11:00am UTC](https://discourse.slicer.org/t/nifti-segmentation-to-dicom-seg-in-a-batch-with-python/32488/3 "2023-10-31T11:00:13Z")

</div>

Perfect! That really helped.  
I now updated the code to:

import slicer  
from DICOMLib import DICOMUtils  
import DICOMScalarVolumePlugin  
import DICOMSegmentationPlugin

niftiPath = r’path\_to\_segmentation\_in\_nifti’  
dicomDataDir = r’path\_to\_CT\_in\_dicom’  
outputFolder = r’path\_to\_output\_folder’

#Load the NIfTI segmentation  
segmentationNode = slicer.util.loadSegmentation(niftiPath)  
segmentationNode.SetAttribute(‘DICOM.SeriesDescription’, ‘Segmentation Series Description’) # Customize the description  
segmentationNode.SetAttribute(‘DICOM.Modality’, ‘SEG’)

loadedNodeIDs = # this list will contain the list of all loaded node IDs  
db = slicer.dicomDatabase  
DICOMUtils.importDicom(dicomDataDir, db)  
patientUIDs = db.patients()  
for patientUID in patientUIDs:  
loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

#Associate segmentation node with a reference volume node  
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)  
referenceVolumeNode = slicer.mrmlScene.GetNodeByID(loadedNodeIDs[0])  
referenceVolumeShItem = shNode.GetItemByDataNode(referenceVolumeNode)

#Set the reference volume for the segmentation  
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(referenceVolumeNode)  
studyShItem = shNode.GetItemParent(referenceVolumeShItem)  
segmentationShItem = shNode.GetItemByDataNode(segmentationNode)  
shNode.SetItemParent(segmentationShItem, studyShItem)

#Export to DICOM  
exporter = DICOMSegmentationPlugin.DICOMSegmentationPluginClass()  
exportables = exporter.examineForExport(segmentationShItem)  
for exp in exportables:  
exp.directory = outputFolder

exporter.export(exportables)

However, the export does not seem to work properly yet. The modality is not set to SEG, and if I want to open it in a different software program, it is not recognized as a segmentation.

What I do manually is, create a new subject with a new study, link the CT and the segmentations. Then, in segment editor I change the source geometry of the segmentations to the CT, and then export the segmentations with the DICOMSegmentationPlugin.

I cannot really find a way to do that with Python. Does someone have a suggestion?
