-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivide_dicom_series_to_folders.py
More file actions
55 lines (39 loc) · 1.39 KB
/
divide_dicom_series_to_folders.py
File metadata and controls
55 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import SimpleITK as sitk
import os
import shutil
'''
A python script for convert dicom files to nrrd file with sepreating contrast.
If your dicom files include 5 different contrast images, using this script,
you will get 5 nrrd files with different contrast!
:Python version: v3.9.0
:Dependency: pip install SimpleITK
:Author: Linkun Gao
'''
def dcmseries_divider(filepath, output_dir):
datapath = filepath
allFilesPath = []
max = 0
contrstIdx = 0
dcms_name = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(datapath)
for dcm in dcms_name:
dicom = sitk.ReadImage(dcm)
instanceNum = int(dicom.GetMetaData('0020|0012'))
if max < instanceNum:
max = instanceNum
for i in range(max):
allFilesPath.append([])
for dcm in dcms_name:
dicom = sitk.ReadImage(dcm)
instanceNum = int(dicom.GetMetaData('0020|0012')) - 1
allFilesPath[instanceNum].append(dcm)
copy_dcms_by_index(allFilesPath, output_dir)
def copy_dcms_by_index(allFilesPath, output_dir):
os.makedirs(output_dir, exist_ok=True)
for idx, paths in enumerate(allFilesPath):
folder = os.path.join(output_dir, str(idx))
os.makedirs(folder, exist_ok=True)
for src in paths:
shutil.copy(src, folder)
filepath = "/Your/dicom/files/path"
output_dir = "/Your/dicom/files/output"
dcmseries_divider(filepath, output_dir)