Last active
November 15, 2020 00:50
-
-
Save medihack/cf2bc6506bb09293d5ffb5b79e6b7322 to your computer and use it in GitHub Desktop.
Modify DICOM files
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
import argparse | |
import os | |
import pydicom | |
from pathlib import Path | |
parser = argparse.ArgumentParser() | |
parser.add_argument('input_folder', help='The DICOM input folder.') | |
parser.add_argument('output_folder', help='The DICOM output folder.') | |
parser.add_argument('-m', '--modify_dicom_tag', help='Modifies a DICOM tag.', type=str, nargs=2, action='append') | |
args = parser.parse_args() | |
modalities = [] | |
for root, dirs, files in os.walk(args.input_folder): | |
for file in files: | |
filepath = os.path.join(root, file) | |
ds = pydicom.dcmread(filepath) | |
modalities.append(ds.Modality) | |
modalities = ','.join(list(set(modalities))) | |
for root, dirs, files in os.walk(args.input_folder): | |
for file in files: | |
filepath = os.path.join(root, file) | |
ds = pydicom.dcmread(filepath) | |
if args.modify_dicom_tag: | |
for modification in args.modify_dicom_tag: | |
tag_to_modify = modification[0] | |
value_to_assign = modification[1] | |
ds[tag_to_modify].value = value_to_assign | |
patient_id = ds.PatientID | |
study_name = f'{ds.StudyDate}_{ds.StudyTime}_{modalities}' | |
series_descr = ds.SeriesDescription | |
outpath = os.path.join(args.output_folder, patient_id, study_name, series_descr) | |
Path(outpath).mkdir(parents=True, exist_ok=True) | |
ds.save_as(os.path.join(outpath, file)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment