Load DICOM series using python

Hi

I am trying to create a python script that can import a DICOM image series from a folder, however I can’t seem to find a way to make this work.

I can load a single DICOM file (in my case an RTSTRUCT) using this code:

contour_fn = 'contours.dcm'
	
	# Add filename to list for DICOM-RT module
	contour_vtkFileList = vtk.vtkStringArray()
	contour_vtkFileList.InsertNextValue(slicer.util.toVTKString(contour_fn))
	
	# Examine files
	contour_loadablesCollection = vtk.vtkCollection()
	slicer.modules.dicomrtimportexport.logic().ExamineForLoad(contour_vtkFileList, contour_loadablesCollection)
	# Set name for when loaded
	contour_loadablesCollection.GetItemAsObject(0).SetName('RTSTRUCT')
	
	# Load file
	contour_success = slicer.modules.dicomrtimportexport.logic().LoadDicomRT(contour_loadablesCollection.GetItemAsObject(0))

However, if I create a list of filenames to load, it doesn’t seem to work correctly. Specifically, my ‘loadablesCollection’ is empty.

Is there a way to do this? I have seen options for loading patients from the DICOM browser, but I don’t want to have to import all the data through that if possible. Also, I’m not sure what the patient name will be, so don’t know how I would do a load by name/ID.

Many thanks

Ben

1 Like

Hi Ben,

There are many convenience functions just for this in DICOMUtils. So you don’t need to go through the logic classes:

I see you want to load RT data. The code form this automated test might help:


or this

1 Like

Hi Csaba

Thanks for your response, I’ll take a look through the examples you suggested.

You can also take a look at this python module that provides a command line tool to essentially take a folder with DICOM data, and organize it into patient/study/series hierarchy of folders accompanied by volumetric reconstructions for each series. It does not handle RT, but it should be easy to add that. You can also find more details here: How can I convert DICOM series to NRRD files in batch mode?.

Hi, i am trying to import a dicom series directory by python scripting, i just found the command 'slicer.util.loadNodeFromFile /loadScene/loadVolume ’ , is there any other function to load the dicom series from directory ?

The post above has been moved here, as the same question has been discussed in this topic. If you have any follow-up questions please post them here.