https://itk.org/ITKExamples/src/IO/GDCM/ReadDICOMSeriesAndWrite3DImage/Documentation.html
#!/usr/bin/env python import sys import os import itk import argparse parser = argparse.ArgumentParser(description="Read DICOM Series And Write 3D Image.") parser.add_argument( "dicom_directory", nargs="?", help="If DicomDirectory is not specified, current directory is used", ) parser.add_argument("output_image", nargs="?") parser.add_argument("series_name", nargs="?") args = parser.parse_args() # current directory by default dirName = "." if args.dicom_directory: dirName = args.dicom_directory PixelType = itk.ctype("signed short") Dimension = 3 ImageType = itk.Image[PixelType, Dimension] namesGenerator = itk.GDCMSeriesFileNames.New() namesGenerator.SetUseSeriesDetails(True) namesGenerator.AddSeriesRestriction("0008|0021") namesGenerator.SetGlobalWarningDisplay(False) namesGenerator.SetDirectory(dirName) seriesUID = namesGenerator.GetSeriesUIDs() if len(seriesUID) < 1: print("No DICOMs in: " + dirName) sys.exit(1) print("The directory: " + dirName) print("Contains the following DICOM Series: ") for uid in seriesUID: print(uid) seriesFound = False for uid in seriesUID: seriesIdentifier = uid if args.series_name: seriesIdentifier = args.series_name seriesFound = True print("Reading: " + seriesIdentifier) fileNames = namesGenerator.GetFileNames(seriesIdentifier) reader = itk.ImageSeriesReader[ImageType].New() dicomIO = itk.GDCMImageIO.New() reader.SetImageIO(dicomIO) reader.SetFileNames(fileNames) reader.ForceOrthogonalDirectionOff() writer = itk.ImageFileWriter[ImageType].New() outFileName = os.path.join(dirName, seriesIdentifier + ".nrrd") if args.output_image: outFileName = args.output_image writer.SetFileName(outFileName) writer.UseCompressionOn() writer.SetInput(reader.GetOutput()) print("Writing: " + outFileName) writer.Update() if seriesFound: break