scientific-skills/pydicom/references/transfer_syntaxes.md
This document provides a comprehensive reference for DICOM transfer syntaxes and compression formats. Transfer syntaxes define how DICOM data is encoded, including byte ordering, compression method, and other encoding rules.
A Transfer Syntax UID specifies:
pydicom.uid.ImplicitVRLittleEndianUsage:
import pydicom
ds.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
pydicom.uid.ExplicitVRLittleEndianUsage:
ds.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian
pydicom.uid.ExplicitVRBigEndianpydicom.uid.JPEGBaseline8BitDependencies: Requires pylibjpeg or pillow
Usage:
# Compress
ds.compress(pydicom.uid.JPEGBaseline8Bit)
# Decompress
ds.decompress()
pydicom.uid.JPEGExtended12Bitpydicom.uid.JPEGLosslessDependencies: Requires pylibjpeg-libjpeg or gdcm
pydicom.uid.JPEGLosslessSV1Usage:
# Compress to JPEG Lossless
ds.compress(pydicom.uid.JPEGLossless)
pydicom.uid.JPEGLSLosslessDependencies: Requires pylibjpeg-libjpeg or gdcm
pydicom.uid.JPEGLSNearLosslesspydicom.uid.JPEG2000LosslessDependencies: Requires pylibjpeg-openjpeg, gdcm, or pillow
Usage:
# Compress to JPEG 2000 Lossless
ds.compress(pydicom.uid.JPEG2000Lossless)
pydicom.uid.JPEG2000Dependencies: Requires pylibjpeg-openjpeg, gdcm, or pillow
pydicom.uid.JPEG2000MCLosslesspydicom.uid.JPEG2000MCpydicom.uid.RLELosslessDependencies: Built into pydicom (no additional packages needed)
Usage:
# Compress with RLE
ds.compress(pydicom.uid.RLELossless)
# Decompress
ds.decompress()
pydicom.uid.DeflatedExplicitVRLittleEndianpydicom.uid.MPEG2MPMLpydicom.uid.MPEG2MPHLimport pydicom
ds = pydicom.dcmread('image.dcm')
# Get transfer syntax UID
ts_uid = ds.file_meta.TransferSyntaxUID
print(f"Transfer Syntax UID: {ts_uid}")
# Get human-readable name
print(f"Transfer Syntax Name: {ts_uid.name}")
# Check if compressed
print(f"Is compressed: {ts_uid.is_compressed}")
# Check if little endian
if ts_uid.is_little_endian:
print("Little Endian")
# Check if implicit VR
if ts_uid.is_implicit_VR:
print("Implicit VR")
# Check compression type
if 'JPEG' in ts_uid.name:
print("JPEG compressed")
elif 'JPEG2000' in ts_uid.name:
print("JPEG 2000 compressed")
elif 'RLE' in ts_uid.name:
print("RLE compressed")
Pydicom can automatically decompress pixel data when accessing pixel_array:
import pydicom
# Read compressed DICOM
ds = pydicom.dcmread('compressed.dcm')
# Pixel data is automatically decompressed
pixel_array = ds.pixel_array # Decompresses if needed
import pydicom
ds = pydicom.dcmread('compressed.dcm')
# Decompress in-place
ds.decompress()
# Now save as uncompressed
ds.save_as('uncompressed.dcm', write_like_original=False)
import pydicom
ds = pydicom.dcmread('uncompressed.dcm')
# Compress using JPEG 2000 Lossless
ds.compress(pydicom.uid.JPEG2000Lossless)
ds.save_as('compressed_j2k.dcm')
# Compress using RLE Lossless (no additional dependencies)
ds.compress(pydicom.uid.RLELossless)
ds.save_as('compressed_rle.dcm')
# Compress using JPEG Baseline (lossy)
ds.compress(pydicom.uid.JPEGBaseline8Bit)
ds.save_as('compressed_jpeg.dcm')
import pydicom
from pydicom.encoders import JPEGLSLosslessEncoder
ds = pydicom.dcmread('uncompressed.dcm')
# Compress with custom parameters
ds.compress(pydicom.uid.JPEGLSLossless, encoding_plugin='pylibjpeg')
Different transfer syntaxes require different Python packages:
pip install pylibjpeg pylibjpeg-libjpeg
# Or
pip install pillow
pip install pylibjpeg pylibjpeg-libjpeg
# Or
pip install python-gdcm
pip install pylibjpeg pylibjpeg-openjpeg
# Or
pip install python-gdcm
# Or
pip install pillow
No additional packages needed - built into pydicom
# Install all common handlers
pip install pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg python-gdcm
import pydicom
# List available pixel data handlers
from pydicom.pixel_data_handlers.util import get_pixel_data_handlers
handlers = get_pixel_data_handlers()
print("Available handlers:")
for handler in handlers:
print(f" - {handler.__name__}")
write_like_original=TrueCause: Missing compression handler Solution: Install the appropriate package (see Installing Compression Handlers above)
Cause: Rare or unsupported compression format
Solution: Try installing python-gdcm which supports more formats
Cause: May need to apply VOI LUT or rescale
Solution: Use apply_voi_lut() or apply RescaleSlope/RescaleIntercept