-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_accession_numbers
37 lines (28 loc) · 1.21 KB
/
extract_accession_numbers
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
import os
import pydicom as dcm
from pydicom import dcmread
def get_accession_numbers(valid_dicom_folder):
accession_numbers = []
# List all DICOM files in the valid_dicom_folder
for root, _, files in os.walk(valid_dicom_folder):
for file in files:
if file.lower().endswith(".dcm"):
ds = dcmread(os.path.join(root, file))
accession_number = ds.AccessionNumber
if accession_number:
accession_numbers.append(accession_number)
return accession_numbers
def save_accession_numbers(accession_numbers, output_file):
with open(output_file, 'w') as file:
for accession_number in accession_numbers:
file.write(accession_number + '\n')
# Replace 'valid_dicom_folder' with the path to your folder containing valid DICOM files
valid_dicom_folder = ''
# Get accession numbers
accession_numbers = get_accession_numbers(valid_dicom_folder)
# Specify the path for the output file
output_file = ''
# Save the accession numbers to a text file
save_accession_numbers(accession_numbers, output_file)
# Accession numbers are now saved in 'accession_numbers.txt'
print(f"Accession numbers have been saved to {output_file}")