-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrenamealldicoms
More file actions
executable file
·32 lines (28 loc) · 1.15 KB
/
renamealldicoms
File metadata and controls
executable file
·32 lines (28 loc) · 1.15 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
#!/usr/bin/env python
import os
import sys
import pydicom
from string import maketrans # Required to call maketrans function.
# read in the datafile
if len(sys.argv) != 2:
print("usage: renamealldicoms directoryname")
exit()
datadirectory = sys.argv[1]
print("renamealldicoms: processing ", datadirectory)
alldirfiles = os.listdir(datadirectory)
dicomstorename = [thefilename for thefilename in alldirfiles if (thefilename.startswith("MR"))]
# first rename files at the top level to get rid of spaces
for thename in dicomstorename:
plan = pydicom.read_file(datadirectory + "/" + thename)
seriesnostr = plan.SeriesNumber
imagenostr = plan.AcquisitionNumber
protocolname = plan.SeriesDescription
tempstring = protocolname + "_" + str(seriesnostr)
thelist = tempstring.split()
dirname = datadirectory + "/" + ''.join(thelist)
outfilename = "IM-" + str(seriesnostr).rjust(4, '0') + "-" + str(imagenostr).rjust(4, '0') + ".dcm"
print(dirname, outfilename)
if not os.path.isdir(dirname + "/"):
os.mkdir(dirname + "/")
namestring = dirname + "/" + outfilename
os.rename(datadirectory + "/" + thename, namestring)