A pure Python package for reading and writing DICOM data
npx @tessl/cli install tessl/pypi-pydicom@3.0.0A pure Python package for reading and writing DICOM (Digital Imaging and Communications in Medicine) files. PyDicom provides comprehensive functionality for working with medical imaging data, offering both high-level convenience functions for common tasks and low-level access for advanced DICOM manipulation.
pip install pydicomimport pydicomMost commonly used imports:
from pydicom import dcmread, dcmwrite
from pydicom.dataset import Dataset, FileDataset, FileMetaDataset
from pydicom.dataelem import DataElement
from pydicom.sequence import SequenceFor pixel data operations:
from pydicom import pixel_array, iter_pixels
# Or more specific:
from pydicom.pixels.utils import pixel_array, iter_pixels
from pydicom.pixels import compress, decompressFor configuration and utilities:
from pydicom import config
from pydicom.config import debug, DS_numpy, DS_decimal
from pydicom.datadict import dictionary_description, keyword_for_tag, tag_for_keywordFor advanced usage:
from pydicom.tag import Tag
from pydicom.uid import UID, generate_uid
from pydicom.valuerep import PersonName, DA, TM, DT
from pydicom.data import get_testdata_filefrom pydicom import dcmread, dcmwrite
from pydicom.dataset import Dataset
# Read a DICOM file
dataset = dcmread("path/to/file.dcm")
# Access DICOM elements
patient_name = dataset.PatientName
study_date = dataset.StudyDate
# Modify elements
dataset.PatientName = "Anonymous"
dataset.PatientID = "12345"
# Add new elements
dataset.add_new(0x0010, 0x1030, "DS", "75.5") # Patient Weight
# Save to new file
dcmwrite("output.dcm", dataset)
# Work with pixel data (requires NumPy)
if hasattr(dataset, 'pixel_array'):
pixels = dataset.pixel_array
print(f"Image shape: {pixels.shape}")
print(f"Data type: {pixels.dtype}")PyDicom follows the DICOM standard structure and provides a comprehensive object model:
The library supports the complete DICOM standard including advanced features like structured reporting, overlays, waveforms, and extensive pixel data processing capabilities.
Core functionality for reading and writing DICOM files with support for various transfer syntaxes, partial reading, and metadata extraction.
def dcmread(fp, force=False, specific_tags=None, stop_before_pixels=False): ...
def dcmwrite(filename, dataset, write_like_original=True): ...Comprehensive dataset management including element access, modification, validation, and serialization with full support for the DICOM data model.
class Dataset:
def __getitem__(self, key): ...
def __setitem__(self, key, value): ...
def add(self, data_element): ...
def add_new(self, tag, VR, value): ...
def save_as(self, filename): ...Individual DICOM data element handling with support for all Value Representations (VRs) and proper type conversion.
class DataElement:
def __init__(self, tag, VR, value): ...
@property
def keyword(self): ...
@property
def description(self): ...Advanced pixel data manipulation including array extraction, compression, decompression, color space conversion, and various image processing operations.
def pixel_array(dataset): ...
def iter_pixels(dataset): ...
def compress(dataset, transfer_syntax_uid): ...
def apply_windowing(arr, dataset): ...DICOM tag and UID management with dictionary lookups, keyword conversion, and UID generation capabilities.
class Tag:
def __init__(self, *args): ...
class UID(str):
def is_transfer_syntax(self): ...
def generate_uid(): ...Specialized handling for DICOM Value Representations including dates, times, person names, and numeric types with proper validation and conversion.
class PersonName:
@property
def family_name(self): ...
@property
def given_name(self): ...
class DA: # Date
def __init__(self, val): ...
class TM: # Time
def __init__(self, val): ...Management of DICOM sequences and multi-value elements with proper nesting and validation support.
class Sequence(list):
def __init__(self, iterable=None): ...
class MultiValue:
def __init__(self, type_constructor, iterable): ...Package configuration, debugging tools, data dictionary access, and various utility functions for DICOM file analysis and manipulation.
def debug(debug_on=True): ...
def dictionary_description(tag): ...
def keyword_for_tag(tag): ...
def tag_for_keyword(keyword): ...from typing import Union, Optional, List, Dict, Any, BinaryIO, Tuple
from pathlib import Path
# Core types
TagType = Union[int, Tuple[int, int], str]
VRType = str
ValueType = Any
FilePathType = Union[str, Path, BinaryIO]
# Pixel data types
ArrayType = Any # NumPy ndarray when available
CompressionType = str
TransferSyntaxType = str