Save files from loaded node ID in batch mode

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?

Here is a slightly older example that just saves one node. You could extend this to loop over your loaded nodes and save them out.

You need to do something like this:

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

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.

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

2 Likes