# Loading DICOM files with python script for Mac

**URL:** https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855
**Category:** Support
**Created:** [August 4, 2020, 4:52pm UTC](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855 "2020-08-04T16:52:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![epearlstone](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@epearlstone](https://discourse.slicer.org/u/epearlstone)
#### Post date: [August 4, 2020, 4:52pm UTC](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855/1 "2020-08-04T16:52:38Z")

</div>

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

---

<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: [August 4, 2020, 4:54pm UTC](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855/2 "2020-08-04T16:54:32Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![epearlstone](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@epearlstone](https://discourse.slicer.org/u/epearlstone)
#### Post date: [August 4, 2020, 5:43pm UTC](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855/3 "2020-08-04T17:43:29Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_load_DICOM_files_into_the_scene_from_a_folder) 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.

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

```

---

<div class="post-metadata">

### Author: ![epearlstone](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@epearlstone](https://discourse.slicer.org/u/epearlstone)
#### Post date: [August 4, 2020, 5:45pm UTC](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855/4 "2020-08-04T17:45:22Z")

</div>

![Screen Shot 2020-08-04 at 1.40.38 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/4/d404bdb69e163e8bc21b3881711525382af2c0f8.png)

---

<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: [August 4, 2020, 6:00pm UTC](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855/5 "2020-08-04T18:00:15Z")

</div>

Thanks for the additional information.

By using the code snippet [above](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855/3), 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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_import_DICOM_files_into_the_application.27s_DICOM_database).

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.

---

<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: [August 4, 2020, 6:15pm UTC](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855/6 "2020-08-04T18:15:02Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![epearlstone](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@epearlstone](https://discourse.slicer.org/u/epearlstone)
#### Post date: [August 4, 2020, 6:30pm UTC](https://discourse.slicer.org/t/loading-dicom-files-with-python-script-for-mac/12855/7 "2020-08-04T18:30:23Z")

</div>

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!
