# Import data automatically to a volume node from a folder using command line

**URL:** <https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753>\
**Category:** Development\
**Created:** [March 19, 2020, 8:41pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753 "2020-03-19T20:41:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 19, 2020, 8:41pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/1 "2020-03-19T20:41:56Z")

</div>

Operating system: win10  
Slicer version: 4.10.2  
Expected behavior:  
Actual behavior:  
Hi,  
I have two folders containing .IMA files. I want to load them automatically into slicer using the folder paths and then perform registration between them.  
So, I need to do first  
# Load Volume  
[success,movingVolumeNode] = slicer.util.loadVolume(Movinfilename,returnNode=True)  
[success, fixedVolumeNode] = slicer.util.loadVolume(Fixedfilename,returnNode=True)  
And then  
logic = SomeRegistrationLogic()  
…  
logic.registerVolumes(fixedVolumeNode, movingVolumeNode, outputVolumeNode)

But slicer.util.loadVolume takes a file not a directory. So my question is how can I import each of these directories using a python script and get them as volume node ?

---

<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:** [March 19, 2020, 9:04pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/2 "2020-03-19T21:04:47Z")

</div>

Hi -

By .IMA files I guess you mean dicom? You can try some of the methods in [DICOMUtils](https://github.com/Slicer/Slicer/commits/master/Modules/Scripted/DICOMLib/DICOMUtils.py). Also here’s a script that takes a directory of dicoms on the command line and makes volumes out of them. I haven’t tried this in a while and it may need some tweaking for the current Slicer.

> <https://github.com/QIICR/dcmheat/blob/master/docker/SlicerConvert.py>

---

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 19, 2020, 9:53pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/4 "2020-03-19T21:53:04Z")

</div>

Hi @pieper  
The “db” error is fixed.  
It would be db =slicer.app.dicomDatabase().  
However it runs successfully and execute  
`"No scalar volumes parsed from the input DICOM dataset! Aborting."`  
Meaning no nodes!! Do you have any idea?  
However it shows that it is imported in the slicer. But needs to be loaded in the GUI.

---

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 19, 2020, 10:46pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/5 "2020-03-19T22:46:02Z")

</div>

@pieper  
I got another example from [here](https://github.com/Slicer/Slicer/blob/master/Modules/Scripted/DICOMLib/DICOMUtils.py).  
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))  
But before I can check for volume node, it throws error:  
loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))  
TypeError: ‘bool’ object is not iterable

---

<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:** [March 19, 2020, 11:12pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/6 "2020-03-19T23:12:15Z")

</div>

We recently reworked the DICOM browser. You need to use latest Slicer Preview Release for this last example (few days old release should be still OK, several weeks old may work differently).

---

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 19, 2020, 11:20pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/7 "2020-03-19T23:20:17Z")

</div>

Hi @lassoan,  
Do you have any suggestion to fix this code (slicer 4.10.2) or another working example to load dicoms from folder and access as volume node using python scripting ?

---

<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:** [March 19, 2020, 11:55pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/8 "2020-03-19T23:55:59Z")

</div>

Slicer-4.10 will soon be retired and Slicer-4.11 will be promoted to be the new stable release, so I would recommend to use most recent Slicer-4.11 version.

---

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 20, 2020, 9:29am UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/9 "2020-03-20T09:29:46Z")

</div>

@lassoan @pieper  
So I found a simpler way. Simple providing the a single file of the folder in below method do the task. It loads all the files automatically.  
slicer.util.loadVolume(‘single\_file\_of\_folder’) .

Can you please tell me How can I set this loaded volume node as the fixed or moved volume from command line to perform landmark registration? I do not want use the gui.  
Thanks

---

<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:** [March 24, 2020, 7:56pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/10 "2020-03-24T19:56:48Z")

</div>

> [@Tokai](#):
>
> Can you please tell me How can I set this loaded volume node as the fixed or moved volume from command line to perform landmark registration? I do not want use the gui.

Probably easiest to look at the self test code for ideas:

> <https://github.com/Slicer/LandmarkRegistration/blob/master/LandmarkRegistration.py#L984-L1013>

---

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 24, 2020, 8:00pm UTC](https://discourse.slicer.org/t/import-data-automatically-to-a-volume-node-from-a-folder-using-command-line/10753/11 "2020-03-24T20:00:16Z")

</div>

Hi @pieper,  
I followed the exact example and it got solved 🙂  
However, It is important that in my case I had to update the transform node from python script.
