Last active
April 7, 2025 15:41
-
-
Save sarthakpati/45d94c408e377ff168f8e9f816f780f2 to your computer and use it in GitHub Desktop.
Reading and converting DICOM images using SimpleITK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import SimpleITK as sitk | |
input_dicom = '/path/to/input/dicom' | |
output_dicom_dir = '/path/to/output' | |
# create the folder output_dicom_dir | |
series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(input_dicom) | |
if not series_IDs: | |
print("ERROR: given directory \"" + input_dicom + "\" does not contain a valid DICOM series.") | |
sys.exit(1) | |
if len(series_IDs) > 1: | |
print(f"WARNING: more than 1 DICOM series was found in the folder: {input_dicom}") | |
itk_images = [] | |
for i in range(0,len(series_IDs)): | |
series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(input_dicom, series_IDs[i]) | |
series_reader = sitk.ImageSeriesReader() | |
series_reader.SetFileNames(series_file_names) | |
series_reader.MetaDataDictionaryArrayUpdateOn() | |
series_reader.LoadPrivateTagsOn() | |
image_dicom = series_reader.Execute() | |
itk_images.append(image_dicom) | |
sitk.WriteImage(image_dicom, os.path.join(output_dicom_dir, series_IDs[i] + ".nii.gz")) | |
## do fancy processing with itk_images |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment