-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicom_header_correction
80 lines (63 loc) · 2.7 KB
/
dicom_header_correction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
DICOM Header Modifier Script
This script is designed to modify DICOM header values based on the provided new values.
It traverses through a directory containing DICOM files, reads each DICOM file, modifies
the specified header tags with new values, and saves the modified DICOM files.
Dependencies:
- pydicom: Library for working with DICOM files
- os: Module for handling file paths
Functions:
- modify_dicom_header: Function to modify the DICOM header with new values.
- Main Script: Traverses through a directory containing DICOM files, identifies
subdirectories, filters DICOM files starting with 'I', and calls
the modify_dicom_header function for each DICOM file.
Usage:
- Ensure pydicom library is installed (pip install pydicom).
- Modify the 'file_path' variable to specify the directory containing DICOM files.
- Specify the new header values to be set in the 'new_values' dictionary.
- Run the script.
"""
import pydicom
import os
def modify_dicom_header(dicom_path, new_values):
"""
Modifies the DICOM header with new values.
Parameters:
dicom_path (str): Path to the DICOM file.
new_values (dict): Dictionary containing tag-value pairs to be set in the DICOM header.
"""
if os.path.exists(dicom_path):
# Load the DICOM file
dicom_data = pydicom.dcmread(dicom_path)
# Modify the DICOM header with new values
for tag, value in new_values.items():
dicom_data[tag].value = value
# Save the modified DICOM file
dicom_data.save_as(dicom_path)
print("Modified DICOM header for %s" % dicom_path)
else:
print("File or directory not found: %s" % dicom_path)
# Define the file path:
file_path = "Your/file/path"
# Specify the new values you want to set in the header
new_values = {
# "StudyDescription" : "ADBSID",
# "StudyComments" : "ASSESMENTID",
# "AccessionNumber" : "ANCID"
}
SeqList = os.listdir(file_path)
for Seq in SeqList:
if Seq == 'DIRFILE':
print('Skipped the DIRFILE')
else:
subdirectory_path = os.path.join(file_path, Seq)
print('Processing subdirectory: %s' % subdirectory_path)
# Get the list of files in the subdirectory
file_list = os.listdir(subdirectory_path)
# Filter out only the files starting with 'I'
dicom_files = [file for file in file_list if file.startswith('I')]
for dicom_file in dicom_files:
dicom_path = os.path.join(subdirectory_path, dicom_file)
print('Trying to read DICOM file: %s' % dicom_path)
# Call the function to modify the DICOM header
modify_dicom_header(dicom_path, new_values)