-
Notifications
You must be signed in to change notification settings - Fork 0
/
importDicom.py
67 lines (44 loc) · 1.58 KB
/
importDicom.py
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
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
import os
import os.path
import sys
from sys import argv, stderr, exit
import shlex, subprocess
# ******************************************************************************
print "usage: importDicom.py /folder-with-dicoms/.txt"
print " "
def get_immediate_subdirectories(dir):
return [name for name in os.listdir(dir)
if os.path.isdir(os.path.join(dir, name))]
# Open filename list
file_ids = open(sys.argv[1],"r")
try:
for line in file_ids:
# Enter the studyID folder
StudyID = line;
ExamsID = get_immediate_subdirectories(StudyID);
print ExamsID
os.chdir(str(StudyID))
# obtain total filenames in directory
list_files = os.listdir(os.path.abspath(ExamsID))
files_dir = os.path.abspath(StudyID)
os.chdir(files_dir)
print files_dir
# Get total number of files
print len(list_files)
# Iterate and split based on image Series
for filename in list_files:
# get image file names
print filename
findSeries_tag = filename.find ( 'S' );
findImages_tag = filename.find ( 'I' );
# get only the series name
series_name = filename[findSeries_tag:findImages_tag];
if not os.path.exists(str(series_name)):
os.makedirs(str(series_name))
os.chdir(str(series_name))
mv_examFolder = str(files_dir)+'/'+filename
mv_examFolder2 = str(files_dir)+'/'+str(series_name)
print mv_examFolder
mv = subprocess.Popen(['mv', mv_examFolder, mv_examFolder2 ], stdout=subprocess.PIPE)
os.chdir(files_dir)