Python package for working with DICOM files. It allows you to read, modify, and write DICOM data in a Pythonic way. Essential for medical imaging processing, clinical data extraction, and AI in radiology.
DICOM is more than an image; it's a rich data structure containing patient info, spatial orientation, and pixel data. Pydicom provides access to all these tags.
Access tags by name (e.g., ds.PatientName) or ID (ds[0x0010, 0x0010]).
Raw pixel data is stored in PixelData, but should be accessed via pixel_array for NumPy integration.
Strict typing for dates, ages, and decimals ensures data integrity.
import pydicom
from pydicom.data import get_testdata_files
import matplotlib.pyplot as plt
import numpy as np
# 1. Read file
ds = pydicom.dcmread("scan.dcm")
# 2. Access Metadata
print(f"Patient: {ds.PatientName}, ID: {ds.PatientID}")
print(f"Modality: {ds.Modality}") # CT, MR, DX
print(f"Study Date: {ds.StudyDate}")
print(f"Slice Thickness: {ds.SliceThickness}")
# 3. Access Image
plt.imshow(ds.pixel_array, cmap="gray")
plt.title(f"{ds.Modality} - {ds.PatientName}")
ds.pixel_array rather than ds.PixelData for proper NumPy integration.hasattr(ds, 'TagName') before accessing optional tags.import pydicom
from pathlib import Path
# Load a series of DICOM files
dicom_dir = Path("dicom_series")
files = sorted(dicom_dir.glob("*.dcm"))
# Load and stack slices
slices = [pydicom.dcmread(f) for f in files]
volume = np.stack([s.pixel_array for s in slices])
# Remove patient identifiers
ds.PatientName = "ANONYMOUS"
ds.PatientID = "000000"
ds.PatientBirthDate = ""
ds.PatientSex = ""
Pydicom is the foundation of medical imaging in Python, enabling researchers and clinicians to work with the rich, standardized DICOM format that powers modern radiology.