19 """ A SimpleITK example demonstrating how to convert and resize DICOM files
20 to common image types. """
26 import multiprocessing
30 import SimpleITK
as sitk
33 def convert_image(input_file_name, output_file_name, new_width=None):
34 """ Convert a single DICOM image to a common image type. """
38 image_file_reader.SetImageIO(
"GDCMImageIO")
39 image_file_reader.SetFileName(input_file_name)
40 image_file_reader.ReadImageInformation()
41 image_size = list(image_file_reader.GetSize())
42 if len(image_size) == 3
and image_size[2] == 1:
44 image_file_reader.SetExtractSize(image_size)
45 image = image_file_reader.Execute()
47 original_size = image.GetSize()
48 original_spacing = image.GetSpacing()
50 (original_size[0] - 1) * original_spacing[0] / (new_width - 1)
54 int((original_size[1] - 1) * original_spacing[1] / new_spacing[1]),
60 interpolator=sitk.sitkLinear,
61 outputOrigin=image.GetOrigin(),
62 outputSpacing=new_spacing,
63 outputDirection=image.GetDirection(),
65 outputPixelType=image.GetPixelID(),
73 if image.GetNumberOfComponentsPerPixel() == 1:
75 if image_file_reader.GetMetaData(
"0028|0004").strip() ==
"MONOCHROME1":
84 def convert_images(input_file_names, output_file_names, new_width):
85 """ Convert multiple DICOM images in parallel to a common image type. """
87 with multiprocessing.Pool(processes=MAX_PROCESSES)
as pool:
89 functools.partial(convert_image, new_width=new_width),
90 zip(input_file_names, output_file_names),
94 def positive_int(int_str):
95 """ Custom argparse type for positive integers. """
98 raise argparse.ArgumentTypeError(int_str +
" is not a positive integer value")
102 def directory(dir_name):
103 """ Custom argparse type for directory. """
104 if not os.path.isdir(dir_name):
105 raise argparse.ArgumentTypeError(dir_name +
" is not a valid directory name")
110 """ Main function. """
111 parser = argparse.ArgumentParser(
112 description=
"Convert and resize DICOM files to common image types."
115 "root_of_data_directory",
117 help=
"Path to the topmost directory containing data.",
120 "output_file_extension",
121 help=
"Image file extension, this determines output file type " "(e.g. png) .",
123 parser.add_argument(
"--w", type=positive_int, help=
"Width of converted images.")
124 parser.add_argument(
"--od", type=directory, help=
"Output directory.")
125 args = parser.parse_args(argv)
127 input_file_names = []
128 for dir_name, _, file_names
in os.walk(args.root_of_data_directory):
129 input_file_names += [
130 os.path.join(os.path.abspath(dir_name), fname)
for fname
in file_names
136 os.path.join(os.path.abspath(args.od), str(i))
137 for i
in range(len(input_file_names))
140 file_names = input_file_names
141 output_file_names = [
142 file_name +
"." + args.output_file_extension
for file_name
in file_names
145 res = convert_images(input_file_names, output_file_names, args.w)
146 input_file_names = list(itertools.compress(input_file_names, res))
147 output_file_names = list(itertools.compress(output_file_names, res))
152 dir_name = args.od
if args.od
else os.getcwd()
153 with open(os.path.join(dir_name,
"file_names.csv"), mode=
"w", encoding=
'utf-8')
as fp:
154 fp_writer = csv.writer(
155 fp, delimiter=
",", quotechar=
'"', quoting=csv.QUOTE_MINIMAL
157 fp_writer.writerow([
"input file name",
"output file name"])
158 for data
in zip(input_file_names, output_file_names):
159 fp_writer.writerow(data)
162 if __name__ ==
"__main__":