【发布时间】:2021-01-26 16:10:13
【问题描述】:
我正在使用一个程序,它将存储在两个不同文件夹(输入文件夹)中的 dicom 文件转换为 nifti 格式,并将它们保存到第三个文件夹(输出文件夹)。目前这个程序需要我手动指定这三个文件夹的文件路径。
我有一个目录,其中包含多个患者/受试者,每个都包含两个(输入)文件夹。第三个(输出)文件夹不存在,但我已将 os.makedirs 添加到上述程序中,因此它会创建它。
我想创建一个可以自动执行此过程的脚本,这样我就不必为每个患者手动指定 3 个文件路径。我希望它获取目录中的每个患者文件夹并应用上述内容 - 即为每个患者读取两个输入文件夹中的 dicom 文件,然后将转换后的 nifti 文件保存到该特定患者的新文件夹中。
#Importing libraries and modules:
import os
from dcmrtstruct2nii import dcmrtstruct2nii, list_rt_structs
#Next I want to create a for-loop that will walk through the directory patient folders/subfolders
#and that can then feed into the below section to automatically specify the RTstructdicom folder, the imagedicom folder
#and that can create a new folder called "Nifti" that can store the output.
for folderName, subfolders, filenames in os.walk('/Users/sh/Drive/folderC'):
#**This is where I'm stuck**
#This next section will create the specified outputfolder e.g. 'Nifti',
#Then load the RTstruct dicom from the specified folder,
#Then load the images dicoms from the specified folder,
#Then convert both to Nifti files saved in the specified output folder
RTstructdicom = ('/Users/sh/Drive/folderC/patient001/subfolder 1/RTSTRUCT_20210124_205145/RTSTRUCT/RTSTRUCT_20210124_205145.dcm')
imagedicoms = ('/Users/sh/Drive/folderC/patient001/subfolder 1/10022/DICOM')
outputfolder = ('/Users/sh/Drive/folderC/patient001/subfolder 1/10022/NIFTI')
print(list_rt_structs(RTstructdicom))
os.makedirs(outputfolder)
dcmrtstruct2nii(RTstructdicom, imagedicoms, outputfolder )
【问题讨论】:
标签: python loops directory-structure os.walk python-os