# Save files from loaded node ID in batch mode

**URL:** https://discourse.slicer.org/t/save-files-from-loaded-node-id-in-batch-mode/11511
**Category:** Support
**Tags:** dicom
**Created:** [May 12, 2020, 11:55pm UTC](https://discourse.slicer.org/t/save-files-from-loaded-node-id-in-batch-mode/11511 "2020-05-12T23:55:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rickychen](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@rickychen](https://discourse.slicer.org/u/rickychen)
#### Post date: [May 12, 2020, 11:55pm UTC](https://discourse.slicer.org/t/save-files-from-loaded-node-id-in-batch-mode/11511/1 "2020-05-12T23:55:07Z")

</div>

Hi guys,

I’m trying to follow the source codes for loading DICOM files as here:

Example: load all data from a DICOM folder (using a temporary DICOM database)  
dicomDataDir = “c:/my/folder/with/dicom-files” # input folder with DICOM files  
loadedNodeIDs = [] # this list will contain the list of all loaded node IDs  
from DICOMLib import DICOMUtils  
with DICOMUtils.TemporaryDICOMDatabase() as db:  
DICOMUtils.importDicom(dicomDataDir, db)  
patientUIDs = db.patients()  
for patientUID in patientUIDs:  
loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

However, I’m not quite clear what will be the next steps. i.e., how can I use the list of “loadedNodeIDs” to obtain the loadable instances so that I can export to other file format?

---

<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: [May 13, 2020, 12:28am UTC](https://discourse.slicer.org/t/save-files-from-loaded-node-id-in-batch-mode/11511/2 "2020-05-13T00:28:57Z")

</div>

[Here](https://github.com/QIICR/dcmheat/blob/master/docker/SlicerConvert.py) is a slightly older example that just saves one node. You could extend this to loop over your loaded nodes and save them out.

---

<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: [May 13, 2020, 2:23am UTC](https://discourse.slicer.org/t/save-files-from-loaded-node-id-in-batch-mode/11511/3 "2020-05-13T02:23:03Z")

</div>

You need to do something like this:

```auto
for loadedNodeID in loadedNodeIDs:
    slicer.util.saveNode(slicer.mrmlScene.GetNodeByID(loadedNodeID), ...set a filename here...)

```

---

<div class="post-metadata">

### Author: ![rickychen](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@rickychen](https://discourse.slicer.org/u/rickychen)
#### Post date: [May 13, 2020, 4:48am UTC](https://discourse.slicer.org/t/save-files-from-loaded-node-id-in-batch-mode/11511/4 "2020-05-13T04:48:50Z")

</div>

Thanks Andras, the syntax is correct, however, for the DICOM series I tested, it says No patient found with DICOM database UID (u’1’,). However, using DICOMScalarVolumePlugin, I can successfully output the correct file. What I did was the following:  
plugin = slicer.modules.dicomPlugins’DICOMScalarVolumePlugin’  
if plugin:  
loadables = plugin.examineFiles(fileNames)  
volume = plugin.load(loadables[0])  
output\_path = os.path.join(output\_directory,output\_filename+output\_extension)  
try:  
slicer.util.saveNode(volume, output\_path)  
except Exception as e:  
print(e)  
exit()  
I’m not sure if this is robust enough to process most DICOM files as Slicer did.

---

<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: [May 16, 2020, 10:20pm UTC](https://discourse.slicer.org/t/save-files-from-loaded-node-id-in-batch-mode/11511/5 "2020-05-16T22:20:46Z")

</div>

Using the scalar volume plugin directly is fine, as long as you only need to load simple 3D volumes (not 4D volumes, structure sets, segmentation objects, registration objects, etc.).
