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:
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.
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!