-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvertJPGDCM.py
47 lines (44 loc) · 2.02 KB
/
convertJPGDCM.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
import vtk
# echo vtk version info
print "using vtk version", vtk.vtkVersion.GetVTKVersion()
import os
####################################################################
def ConvertJPGDirectory(work_dir,output_dir):
# list the directory
directoryList = os.listdir(work_dir)
# build the list of vtk and vti files
files = filter(lambda x:os.path.isfile("%s/%s" % (work_dir,x) ),directoryList)
jpgFiles = filter(lambda x: x.split(".").pop() == "jpg" ,files)
filenames = jpgFiles
# loop over list of files and convert to matlab
for idlist, fileID in enumerate( filenames):
CurrentInFileName = "%s/%s" % (work_dir ,fileID)
CurrentOutFileName = "%s/%s" % (output_dir,fileID.replace("jpg","dcm"))
convertcmd = "c3d -mcs %s -type uchar -omc %s" % (CurrentInFileName, CurrentOutFileName )
print convertcmd
os.system(convertcmd )
# FIXME - what tags should we change
swapheaderdmc = "DicomImageReadChangeHeaderWrite %s %s '0008|0060' MR '0008|0090' wolfpac '0008|1050' ping" % (CurrentOutFileName, CurrentOutFileName)
print swapheaderdmc
os.system(swapheaderdmc )
print "%d of %d:read %s wrote %s" % (idlist,len(filenames),CurrentInFileName,CurrentOutFileName)
# setup command line parser to control execution
from optparse import OptionParser
parser = OptionParser()
parser.add_option( "--file_dir",
action="store", dest="file_dir", default=None,
help="convert .jpg files in this directory to .dcm files", metavar = "DIR")
parser.add_option( "--output_dir",
action="store", dest="output_dir", default=None,
help="output files to this directory", metavar = "DIR")
(options, args) = parser.parse_args()
if (options.file_dir):
# default output directory to input directory is not set
OutputDirectory = options.file_dir
if (options.output_dir):
OutputDirectory = options.output_dir
os.system("mkdir -p %s" % OutputDirectory )
ConvertJPGDirectory(options.file_dir,OutputDirectory)
else:
parser.print_help()
print options