23 import multiprocessing
27 import SimpleITK
as sitk
30 def convert_image(input_file_name, output_file_name, new_width=None):
34 image_file_reader.SetImageIO(
'GDCMImageIO')
35 image_file_reader.SetFileName(input_file_name)
36 image_file_reader.ReadImageInformation()
37 image_size = list(image_file_reader.GetSize())
38 if len(image_size) == 3
and image_size[2] == 1:
40 image_file_reader.SetExtractSize(image_size)
41 image = image_file_reader.Execute()
43 original_size = image.GetSize()
44 original_spacing = image.GetSpacing()
45 new_spacing = [(original_size[0] - 1) * original_spacing[0]
46 / (new_width - 1)] * 2
47 new_size = [new_width, int((original_size[1] - 1)
48 * original_spacing[1] / new_spacing[1])]
51 interpolator=sitk.sitkLinear,
52 outputOrigin=image.GetOrigin(),
53 outputSpacing=new_spacing,
54 outputDirection=image.GetDirection(),
56 outputPixelType=image.GetPixelID())
63 if image.GetNumberOfComponentsPerPixel() == 1:
65 if image_file_reader.GetMetaData(
'0028|0004').strip() \
75 def convert_images(input_file_names, output_file_names, new_width):
77 with multiprocessing.Pool(processes=MAX_PROCESSES)
as pool:
78 return pool.starmap(functools.partial(convert_image,
80 zip(input_file_names, output_file_names))
83 def positive_int(int_str):
86 raise argparse.ArgumentTypeError(
87 int_str +
' is not a positive integer value')
91 def directory(dir_name):
92 if not os.path.isdir(dir_name):
93 raise argparse.ArgumentTypeError(dir_name +
94 ' is not a valid directory name')
99 parser = argparse.ArgumentParser(
100 description=
'Convert and resize DICOM files to common image types.')
101 parser.add_argument(
'root_of_data_directory', type=directory,
102 help=
'Path to the topmost directory containing data.')
104 'output_file_extension',
105 help=
'Image file extension, this determines output file type '
107 parser.add_argument(
'--w', type=positive_int,
108 help=
'Width of converted images.')
109 parser.add_argument(
'--od', type=directory, help=
'Output directory.')
110 args = parser.parse_args(argv)
112 input_file_names = []
113 for dir_name, subdir_names, file_names
in os.walk(
114 args.root_of_data_directory):
115 input_file_names += [os.path.join(os.path.abspath(dir_name), fname)
116 for fname
in file_names]
120 file_names = [os.path.join(os.path.abspath(args.od), str(i))
121 for i
in range(len(input_file_names))]
123 file_names = input_file_names
124 output_file_names = [file_name +
'.' + args.output_file_extension
125 for file_name
in file_names]
127 res = convert_images(input_file_names, output_file_names, args.w)
128 input_file_names = list(itertools.compress(input_file_names, res))
129 output_file_names = list(itertools.compress(output_file_names, res))
134 dir_name = args.od
if args.od
else os.getcwd()
135 with open(os.path.join(dir_name,
'file_names.csv'), mode=
'w')
as fp:
136 fp_writer = csv.writer(fp, delimiter=
',', quotechar=
'"',
137 quoting=csv.QUOTE_MINIMAL)
138 fp_writer.writerow([
'input file name',
'output file name'])
139 for data
in zip(input_file_names, output_file_names):
140 fp_writer.writerow(data)
143 if __name__ ==
"__main__":