pylibjpeg-openjpeg

2.5.0 · active · verified Thu Apr 16

pylibjpeg-openjpeg is a Python wrapper for the OpenJPEG library, specifically designed as a plugin for pylibjpeg. It enables JPEG 2000 (J2K) decoding and encoding for various image data, including DICOM pixel data. The current version is 2.5.0, and releases often coincide with new Python versions or updates to the underlying OpenJPEG library, typically every few months.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates direct usage of pylibjpeg-openjpeg's `encode_pixel_data` and `decode_pixel_data` functions using NumPy arrays. For DICOM integration, you typically register the plugin with `pylibjpeg` and then use `pylibjpeg.decode()`.

import numpy as np
from pylibjpeg_openjpeg import openjpeg

# Example: Encoding a simple NumPy array to JPEG 2000
# For real use, ensure data matches expected type/shape for J2K
width, height = 128, 128
array_data = np.arange(width * height, dtype=np.uint16).reshape((height, width))

# Encode to JPEG 2000 (lossless example)
encoded_data = openjpeg.encode_pixel_data(
    array_data,
    bits_allocated=16,
    bits_stored=16,
    high_bit=15,
    photometric_interpretation='MONOCHROME2',
    samples_per_pixel=1,
    rows=height,
    columns=width,
    j2k_codec='jp2',
    lossless=True
)

print(f"Encoded data length: {len(encoded_data)} bytes")

# Decode the data back
decoded_array, image_format = openjpeg.decode_pixel_data(
    encoded_data,
    rows=height,
    columns=width,
    bits_allocated=16,
    bits_stored=16,
    high_bit=15,
    photometric_interpretation='MONOCHROME2',
    samples_per_pixel=1
)

print(f"Decoded array shape: {decoded_array.shape}, dtype: {decoded_array.dtype}")
assert np.array_equal(array_data, decoded_array)
print("Original and decoded arrays are equal (lossless encoding verified).")

view raw JSON →