pylibjpeg-libjpeg

2.4.0 · active · verified Thu Apr 16

pylibjpeg-libjpeg is a Python wrapper for the `libjpeg` C library, designed primarily to function as a plugin for the `pylibjpeg` framework, which in turn provides JPEG decoding capabilities for `pydicom`. It enables Python applications to decode various JPEG formats, including JPEG Baseline, Extended, Lossless (Process 14), JPEG-LS Lossless, and Lossy. The library is actively maintained, with the current version being 2.4.0, and typically sees updates for Python version compatibility and `libjpeg` upgrades every few months to a year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the primary use case of `pylibjpeg-libjpeg` as a plugin for `pydicom` to decode JPEG-compressed DICOM pixel data. Once installed, `pydicom` (via `pylibjpeg`) automatically discovers and uses `pylibjpeg-libjpeg` for supported JPEG transfer syntaxes.

import pydicom
from pydicom.data import get_testdata_file
import numpy # pylibjpeg-libjpeg returns numpy arrays

# Ensure pylibjpeg-libjpeg is installed: pip install pylibjpeg-libjpeg
# This makes it available as a plugin for pydicom through pylibjpeg

# Load a DICOM file with JPEG-compressed pixel data
# Requires 'pydicom-data' for test files, or use your own DICOM file
# If you don't have test files, uncomment: pydicom.data.fetch_data_files()
dicom_file_path = get_testdata_file('JPEG-LL.dcm') # Example: JPEG Lossless

ds = pydicom.dcmread(dicom_file_path)

# Accessing .pixel_array will trigger decoding via pylibjpeg-libjpeg
# if the transfer syntax is supported by the plugin.
pixel_array = ds.pixel_array

print(f"DICOM file loaded: {dicom_file_path}")
print(f"Transfer Syntax UID: {ds.file_meta.TransferSyntaxUID}")
print(f"Pixel array shape: {pixel_array.shape}")
print(f"Pixel array dtype: {pixel_array.dtype}")
print(f"Type of decoded data: {type(pixel_array)}")

view raw JSON →