Loading DICOM files with python script for Mac

I am having trouble loading in DICOM files using the python script given in the Slicer python script repository. I am using a Mac computer and Slicer 4.11. Does anyone have a solution?

dicomDataDir = “”~/Users/epstone/Desktop/Dicomdirector/Folder" # 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))

Can you tell what your trouble is? What do you do, what do you expect to happen, and what happens instead? Do you see any error messages? Does DICOM import via the GUI works well? Have you tried with the very latest Slicer Preview Release?

I am expecting my folder full of dcm files to be imported as they would normally when using the DICOM import GUI (which works well. I am simply trying to practice using this scripting method). I am using the latest version of Slicer 4.11.0 and Python 3.6.7. I will attach a picture of my Slicer screen. No error is being given with my current implementation but nothing is happening. No files are loaded.
I am using the script provided in the python script repository given below:

How to load DICOM files into the scene from a folder
This code loads all DICOM objects into the scene from a file folder. All the registered plugins are evaluated and the one with the highest confidence will be used to load the data. Files are imported into a temporary DICOM database, so the current Slicer DICOM database is not impacted.

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

Thanks for the additional information.

By using the code snippet above, data is imported into a temporary database (note the DICOMUtils.TemporaryDICOMDatabase() command). If you want to import to the Slicer DICOM database then you can use this code snippet.

If no errors are printed on the console then most likely there is a trivial issue (e.g., wrong input folder, invalid temporary folder). Messages in the application log (menu: Help / Report a bug) may give some hints.

Your script (with slight simplifications) worked fine for me:

folder = r"c:\D\S4\Testing\Data\Input\CTHeadAxialDicom"

import DICOMLib.DICOMUtils as utils
import DICOMScalarVolumePlugin
result = utils.importDicom(folder)
scalarVolumeReader = DICOMScalarVolumePlugin.DICOMScalarVolumePluginClass()
for patient in db.patients():
  for study in db.studiesForPatient(patient):
    for series in db.seriesForStudy(study):
      print('looking at series ' + series + ' for patient ' + patient)
      files = db.filesForSeries(series)
      loadable = scalarVolumeReader.examineForImport([files])[0]
      scalarVolumeReader.load(loadable)

This worked great. I think my original problem was my directory path was not written correctly because I have tried it all three ways and they all work. I will post all three methods for other’s reference.
For loading the dicom files into a temporary database I used:

from DICOMScalarVolumePlugin import DICOMScalarVolumePluginClass

import DICOMLib.DICOMUtils as utils
import slicer
import os.path
folder = os.path.expanduser(“/Users/ethanpearlstone/Desktop/Dicomdirector/For Slicer/CT abd 072610/”)
result = utils.importDicom(folder)
DSVPC = DICOMScalarVolumePluginClass()
db = slicer.dicomDatabase
if (db.patients()):
for patient in db.patients():
for study in db.studiesForPatient(patient):
for series in db.seriesForStudy(study):
print('looking at series ’ + str(series) + ’ for patient ’ + str(patient))
files = db.filesForSeries(series)
DSVPC.load(DSVPC.examineForImport([files])[0])

For importing to the Slicer DICOM database I used:

slicer.util.selectModule(“DICOM”)
dicomBrowser = slicer.modules.DICOMWidget.browserWidget.dicomBrowser
dicomBrowser.importDirectory(“/Users/ethanpearlstone/Desktop/Dicomdirector/For Slicer/CT abd 072610/”, dicomBrowser.ImportDirectoryAddLink)
dicomBrowser.waitForImportFinished()

Using the simplified method @lassoan provided:

folder = “/Users/ethanpearlstone/Desktop/Dicomdirector/For Slicer/CT abd 072610/”

import DICOMLib.DICOMUtils as utils
import DICOMScalarVolumePlugin
result = utils.importDicom(folder)
scalarVolumeReader = DICOMScalarVolumePlugin.DICOMScalarVolumePluginClass()
for patient in db.patients():
for study in db.studiesForPatient(patient):
for series in db.seriesForStudy(study):
print('looking at series ’ + series + ’ for patient ’ + patient)
files = db.filesForSeries(series)
loadable = scalarVolumeReader.examineForImport([files])[0]
scalarVolumeReader.load(loadable)

Thank you for your help!

1 Like