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

Operating system: win10
Slicer version: 4.10.2
Expected behavior:
Actual behavior:
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 ?

Hi -

By .IMA files I guess you mean dicom? You can try some of the methods in DICOMUtils. 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.

Hi @pieper
The “db” error is fixed.
It would be db
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.

I got another example from here.
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:
But before I can check for volume node, it throws error:
TypeError: ‘bool’ object is not iterable

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).

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 ?

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.

@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.

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

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