# Load Dicom series file problems

**URL:** <https://discourse.slicer.org/t/load-dicom-series-file-problems/4160>\
**Category:** Support\
**Tags:** dicom\
**Created:** [September 21, 2018, 9:45am UTC](https://discourse.slicer.org/t/load-dicom-series-file-problems/4160 "2018-09-21T09:45:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_luo](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@jiang\_luo](https://discourse.slicer.org/u/jiang_luo)\
**Post date:** [September 21, 2018, 9:45am UTC](https://discourse.slicer.org/t/load-dicom-series-file-problems/4160/1 "2018-09-21T09:45:57Z")

</div>

Operating system:win7  
Slicer version: 4.9  
Expected behavior:  
Actual behavior:

Hi, I had created the python script that load the series of the DICOM files. the script is follow:

def LoadDicomFile(path=None,NodeName=None):  
filelist=os.listdir(path)  
progress = slicer.util.createProgressDialog(parent=None, value=0, maximum=len(filelist))  
plugin=DICOMScalarVolumePlugin.DICOMScalarVolumePluginClass()  
from DICOMLib import DICOMLoadable  
loadable=DICOMLoadable()  
step=0  
for filename in filelist:  
loadable.files.append(path + ‘/’ + filename)  
labletext = ‘\n LoadDicomFile %s …’ % filename  
progress.labelText = labletext  
slicer.app.processEvents()  
progress.setValue(step)  
step += 1  
slicer.app.processEvents()  
try:  
flag = plugin.load(loadable)  
except:  
print ‘except load false …’  
progress.close()  
del plugin  
del loadable

LoadDicomFile(‘f:/data/’)  
but the MPR seem is not good look this… the script have any problems???

 ![%E6%97%A0%E6%A0%87%E9%A2%98](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/c/3cc7ca57d7d0d76e8a8cfdf9b286be9f43e05baa.jpeg)

---

<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:** [September 21, 2018, 11:43am UTC](https://discourse.slicer.org/t/load-dicom-series-file-problems/4160/2 "2018-09-21T11:43:41Z")

</div>

You get the file list by calling `filelist=os.listdir(path)`, which returns the files in some random order (how they are ordered in the file system). See this topic about how to do it correctly:

> [@Load DICOM series using python](https://discourse.slicer.org/t/load-dicom-series-using-python/3257):
>
> 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…

---

<div class="post-metadata">

**Author:** ![jiang\_luo](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@jiang\_luo](https://discourse.slicer.org/u/jiang_luo)\
**Post date:** [September 21, 2018, 12:41pm UTC](https://discourse.slicer.org/t/load-dicom-series-file-problems/4160/3 "2018-09-21T12:41:50Z")

</div>

> [@lassoan](#):
>
> You get the file list by calling `filelist=os.listdir(path)` , which returns the files in some random order (how they are ordered in the file system). See this topic about how to do it correctly:
> 
> ![|20x20](upload://oBmm92EHQmoqH4ENK3ajlRs6cpw.png [Load DICOM series using python](https://discourse.slicer.org/t/load-dicom-series-using-python/3257) [Support](https://discourse.slicer.org/c/support)

Thanks for your quickly help, i will try the Load the ordered files…

---

<div class="post-metadata">

**Author:** ![Davide\_Cester](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/davide_cester/32/6564_2.png) [@Davide\_Cester](https://discourse.slicer.org/u/Davide_Cester)\
**Post date:** [April 19, 2020, 6:54pm UTC](https://discourse.slicer.org/t/load-dicom-series-file-problems/4160/4 "2020-04-19T18:54:19Z")

</div>

Hi! I’m experimenting with controlling 3DSlicer from Jupyter Notebook and I found the initial script useful… I want to share the Python3 version of it since others may end up here:

```
import os
from glob import glob
from DICOMLib import DICOMLoadable
import DICOMScalarVolumePlugin

def load_dicom_folder(path):
    plugin = DICOMScalarVolumePlugin.DICOMScalarVolumePluginClass()
    loadable = DICOMLoadable()
    for file_path in sorted(glob(os.path.join(path, '*'))):
        loadable.files.append(file_path)
    try:
        flag = plugin.load(loadable)
    except Exception as e:
        print(e)

load_dicom_folder('folderPath')

```

I removed the progress dialog window, since it’s so fast It’s barely useful nowadays.

---

<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:** [April 19, 2020, 8:44pm UTC](https://discourse.slicer.org/t/load-dicom-series-file-problems/4160/5 "2020-04-19T20:44:39Z")

</div>

You can also find up-to-date DICOM loading examples in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_load_DICOM_files_into_the_scene_from_a_folder).

---

<div class="post-metadata">

**Author:** ![Davide\_Cester](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/davide_cester/32/6564_2.png) [@Davide\_Cester](https://discourse.slicer.org/u/Davide_Cester)\
**Post date:** [April 20, 2020, 7:56am UTC](https://discourse.slicer.org/t/load-dicom-series-file-problems/4160/6 "2020-04-20T07:56:06Z")

</div>

I did not find found that page before… the code there is much better  
than mine, with the official script in the Wiki the Patient and the  
Study are also handled! 🙂

Thank you!
