My long goal is to trick a piece of software into thinking it's handling CT data instead of MRI data. So I want to delete all the dicom tags associated with MR data and replace them with CT specific ones. It's using anonymised MRI data for research. This is what I've tried, and it runs fine and seems to edit the files in some capacity, but the tags I'm trying to delete are still there.
import os
import pydicom
def delete_dicom_tags(folder_path, tags_to_delete):
for root, dirs, files in os.walk(folder_path):
for file_name in files:
file_path = os.path.join(root, file_name)
# Load DICOM file
dicom_data = pydicom.dcmread(file_path)
# Create a new dataset without the specified tags
new_dataset = pydicom.Dataset()
for elem in dicom_data:
tag_tuple = (elem.tag.group, elem.tag.element)
if tag_tuple not in tags_to_delete:
new_dataset.add(elem)
# Set attributes for the new dataset
new_dataset.is_little_endian = dicom_data.is_little_endian
new_dataset.is_implicit_VR = dicom_data.is_implicit_VR
# Save modified DICOM file
new_file_path = file_path.replace('.dcm', '_modified.dcm')
new_dataset.save_as(new_file_path)
if __name__ == "__main__":
# Replace 'your_folder_path' with the actual path to your DICOM files folder
folder_path = 'PythonEdittingFolder\InputFolder'
# Specify the DICOM tags to delete (replace with the actual tags you want to delete)
tags_to_delete = [
(0x0018, 0x1000),
(0x0020, 0x0010),
# Add more tags as needed
]
delete_dicom_tags(folder_path, tags_to_delete)
print("DICOM tags deleted successfully.")
The code runs, edits the dicom files. But when I check, the dicom files still have the dicom tags I'm trying to delete.