0% found this document useful (0 votes)
58 views2 pages

Pydicom Example

The document contains a Python script that uses the pydicom library to read a DICOM file and extract patient, study, series, and equipment information. It retrieves various attributes such as patient's name, ID, birth date, study UID, and modality, printing them to the console. The script also includes error handling to manage exceptions when reading the DICOM file.

Uploaded by

Aditi Thakore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

Pydicom Example

The document contains a Python script that uses the pydicom library to read a DICOM file and extract patient, study, series, and equipment information. It retrieves various attributes such as patient's name, ID, birth date, study UID, and modality, printing them to the console. The script also includes error handling to manage exceptions when reading the DICOM file.

Uploaded by

Aditi Thakore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pydicom

from pydicom.data import get_testdata_file

filepath = path = get_testdata_file("rtplan.dcm")


try:
# Attempt to read the DICOM file
ds = pydicom.dcmread(filepath, stop_before_pixels=True)
#print(ds)

# Access Patient Information (Clinical Data)


patient_name = ds.get((0x0010, 0x0010), "Unknown") # Patient's Name
patient_id = ds.get((0x0010, 0x0020), "Unknown") # Patient's ID
patient_birthdate = ds.get((0x0010, 0x0030), "Unknown") # Patient's Birth
Date
patient_sex = ds.get((0x0010, 0x0040), "Unknown") # Patient's Sex

# Access Study Information


study_uid = ds.get((0x0020, 0x000D), "Unknown") # Study Instance UID
study_description = ds.get((0x0008, 0x1030), "Unknown") # Study
Description
study_id = ds.get((0x0020, 0x0010), "Unknown") # Study ID
study_date = ds.get((0x0008, 0x0020), "Unknown") # Study Date
study_time = ds.get((0x0008, 0x0030), "Unknown") # Study Time

# Access Series Information


series_uid = ds.get((0x0020, 0x000E), "Unknown") # Series Instance UID
series_description = ds.get((0x0008, 0x103E), "Unknown") # Series
Description
modality = ds.get((0x0008, 0x0060), "Unknown") # Modality (CT, MR,
etc.)

# Access Equipment Information


manufacturer = ds.get((0x0008, 0x0070), "Unknown") # Manufacturer
model_name = ds.get((0x0008, 0x1090), "Unknown") # Model Name
device_serial_number = ds.get((0x0018, 0x1000), "Unknown") # Device Serial
Number

# Print extracted information


print("Patient Information:")
print(f"Name: {patient_name}")
print(f"ID: {patient_id}")
print(f"Birth Date: {patient_birthdate}")
print(f"Sex: {patient_sex}")

print("\nStudy Information:")
print(f"Study UID: {study_uid}")
print(f"Study Description: {study_description}")
print(f"Study ID: {study_id}")
print(f"Study Date: {study_date}")
print(f"Study Time: {study_time}")

print("\nSeries Information:")
print(f"Series UID: {series_uid}")
print(f"Series Description: {series_description}")
print(f"Modality: {modality}")

print("\nEquipment Information:")
print(f"Manufacturer: {manufacturer}")
print(f"Model: {model_name}")
print(f"Device Serial Number: {device_serial_number}")

except Exception as e:
# If pydicom raises an exception, it's not a valid DICOM file
print(f"Error reading {filepath}: {e}")

# # Step 2: Access basic information from the ds

# # Print Patient's Name


# print("Patient's Name:", ds.PatientName)

# # Print Patient's ID
# print("Patient's ID:", ds.PatientID)

# # Print Study Date


# print("Study Date:", ds.StudyDate)

# # Print Study Time


# print("Study Time:", ds.StudyTime)

# # Print Modality (CT, MRI, etc.)


# print("Modality:", ds.Modality)

# # Step 3: Print the full DICOM ds (for debugging or exploration)


# print("\nDICOM ds:")
# print(ds)

You might also like