DICOM stands for Digital and Communications in Medicine and is used for managing medical data. One of the most common uses of this format is the storage, transfer, and display of diagnostic images like X-rays, CT scans, and MRIs.
While there are variations depending on the type of image and the manufacturer of the equipment that generated it, a DICOM file contains some common elements:
- HEADER: The initial part of the file contains metadata describing its content (patient identification, image acquisition modality, parameters for acquiring the images, equipment manufacturer, etc.).
- ATTRIBUTE GROUPS: The metadata in the header is organized into attribute groups containing a series of DICOM tags that provide specific information. For example, tag 0010,0010 specifies the patient’s name.
- TRANSFER SYNTAX: Specifies how the data is encoded and stored.
- IMAGE: Contains the pixels or voxels that make up the image, which may be compressed or uncompressed.
- TRAILER: Indicates the end of the DICOM file and may be absent.
The main attributes of a DICOM file include:
- PatientName: Patient’s name.
- PatientAge: Patient’s age.
- StudyDate: Date of the study.
- StudyDescription: Study description.
- Modality: Imaging modality used (e.g., CT, MR, X-ray, etc.).
- Manufacturer: Imaging equipment manufacturer.
- Rows: Number of rows in the image.
- Columns: Number of columns in the image.
- PixelData: Image pixel data.
- ImageOrientationPatient: Image orientation relative to the patient.
- ImagePositionPatient: Spatial position of the image relative to the patient.
- SliceThickness: Slice thickness in an imaging volume.
- PixelSpacing: Pixel spacing in the image.
When examining a DICOM file related to angiographic images, the modality will be XA. The study type attribute will specify whether it is coronary, cerebral, or another type of angiography. The sequence type attribute indicates the direction of the subsequent images (anteroposterior, lateral, oblique). The number of images in the sequence is usually indicated by the “NumberOfFrames” tag.
A very useful library for working with DICOM files in Python is pydicom. It is the one we will use for all work on DICOM files.
Before accessing it, you need to install it by running the following command in the terminal:
pip install pydicom
The following program reads the attributes of the DICOM (.dcm) file specified in the “dicom_file_path” variable.
import pydicom
def print_dicom_attributes(dicom_file):
# Load the DICOM file
ds = pydicom.dcmread(dicom_file)
# Iterate over all data elements in the DICOM dataset
for element in ds:
# Extract the tag, name, and value of the DICOM attribute
tag = element.tag
name = element.name
# Print the attribute information
print(f"Tag: {tag}, Name: {name}")
if __name__ == "__main__":
# Specify the path to the DICOM file
dicom_file_path = "path/to/your/dicom/file.dcm"
# Call the function to print DICOM attributes
print_dicom_attributes(dicom_file_path)
The list of attributes obtained is often very long and not very useful.
We can limit the number of attributes to those we are interested in and read their contents. In the following program, we created a dictionary containing some specific attributes and read them:
import pydicom
def print_important_dicom_attributes(dicom_file):
# Load the DICOM file
ds = pydicom.dcmread(dicom_file)
# Define a list of important tags to print
important_tags = {
"PatientName": "Patient's Name",
"PatientID": "Patient's ID",
"PatientBirthDate": "Patient's Birth Date",
"PatientSex": "Patient's Sex",
"StudyID": "Study ID",
"StudyDate": "Study Date",
"StudyTime": "Study Time",
"SeriesNumber": "Series Number",
"Modality": "Modality",
"Rows": "Number of Rows in Image",
"Columns": "Number of Columns in Image",
"NumberOfFrame": "Number of Frames in Sequence
}
# Iterate over the important tags and print their values
for tag, description in important_tags.items():
if tag in ds:
value = ds.data_element(tag).value
print(f"{description} ({tag}): {value}")
else:
print(f"{description} ({tag}): Not Available")
if __name__ == "__main__":
# Specify the path to the DICOM file
dicom_file_path = "path/to/your/dicom/file.dcm"
# Call the function to print important DICOM attributes
print_important_dicom_attributes(dicom_file_path)
The output is as follows:
- Patient’s Name (PatientName): XXXXXX^XXXXXX
- Patient’s ID (PatientID): 000000000000000000
- Patient’s Birth Date (PatientBirthDate): 19000402
- Patient’s Sex (PatientSex): M
- Study ID (StudyID): 2020000
- Study Date (StudyDate): 20200000
- Study Time (StudyTime): 084611.000
- Series Number (SeriesNumber): 1
- Modality (Modality): XA
- Number of Rows in Image (Rows): 512
- Number of Columns in Image (Columns): 512
- Number of Frames in Sequence (NumberOfFrames): 86
In an upcoming article, we will delve into the part of the file containing the image pixels to view and manage them.