Created
November 2, 2023 07:39
-
-
Save zemmyang/610ed7df83a7588e05c5bcd8edef8b6d to your computer and use it in GitHub Desktop.
DICOMpiler
This file contains 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
""" | |
DICOMpiler | |
Splits a folder of DICOM files in one folder into subfolders by series UID | |
Warning: will throw an error if there are non-DICOM files in the folder | |
If you want to split the files according to another identifier, change 0x0020000e into the DICOM tag that you want. | |
Some useful tags to sort by: | |
0x00200011 Series Number | |
0x0020000e Series Instance UID | |
0x00100020 Patient ID | |
0x00080022 Acquisition Date | |
0x00080032 Acquisition Time | |
0x00081030 Study Description | |
0x0008103e Series Description | |
""" | |
from pathlib import Path | |
import pydicom | |
# specify the paths here | |
image_path = r"/source/of/your/images" | |
output_path = r"/destination/of/your/subfolders/output" | |
# looks for files in the folder - not recursive, and ignores all file extensions | |
# if the files are mixed and some have extensions, change the "*" to "*.dcm" or something | |
images = [im for im in Path(image_path).glob("*")] | |
# print the series IDs of the images | |
# print({pydicom.dcmread(im).get(0x0020000e).value for im in images}) | |
# group images by series uid | |
series_grp = {} | |
for im in images: | |
series_uid = pydicom.dcmread(im).get(0x0020000e).value | |
if series_uid not in series_grp: | |
series_grp[series_uid] = [] | |
series_grp[series_uid].append(im.relative_to(image_path)) | |
# copy the files into the folders | |
import shutil | |
for series_uid, images in series_grp.items(): | |
series_path = Path(output_path) / series_uid | |
series_path.mkdir(exist_ok=True, parents=True) | |
for im in images: | |
shutil.copy(Path(image_path) / im, series_path / im.name) | |
print("DONE") |
This file contains 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
""" | |
DICOMverter | |
Takes the output folders of DICOMpiler and turns it into .nii.gz files. | |
Will ignore folders that can't be read by SimpleITK | |
""" | |
from pathlib import Path | |
import SimpleITK as sitk | |
# read the folders in the output folder | |
for folder in Path("output").glob("*"): | |
try: | |
reader = sitk.ImageSeriesReader() | |
dicom_names = reader.GetGDCMSeriesFileNames(str(folder)) | |
reader.SetFileNames(dicom_names) | |
image = reader.Execute() | |
sitk.WriteImage(image, str(folder) + ".nii.gz") | |
except Exception as e: | |
print(folder) | |
print(e) | |
continue | |
print("DONE") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment