PyTurboJPEG: High-performance JPEG Handling

2.2.0 · active · verified Thu Apr 16

PyTurboJPEG is a Python wrapper for the high-performance libjpeg-turbo library, enabling fast decoding and encoding of JPEG images. It leverages the underlying C library for significant speed improvements over pure Python or slower image processing libraries. The library is actively maintained with regular releases, often reflecting updates and features from the upstream libjpeg-turbo project. Current version is 2.2.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize PyTurboJPEG, create a dummy image (using NumPy), encode it into JPEG format, and then decode the JPEG data back into a raw pixel buffer, converting it back to a NumPy array for further processing. It includes error handling guidance for common issues related to the libjpeg-turbo dependency.

from pyturbojpeg import TurboJPEG, TJPF_BGR, TJ_BGR
import numpy as np
import os

# Instantiate TurboJPEG. Optionally, pass the path to libturbojpeg.so.0, .dylib, or .dll
# Example: jpeg = TurboJPEG('/usr/local/lib/libturbojpeg.so.0')
jpeg = TurboJPEG()

# --- Encode Example ---
# Create a dummy BGR image (e.g., from OpenCV or Pillow in BGR mode)
width, height = 640, 480
# For simplicity, create a blank image; in real use, this would be actual image data
# PyTurboJPEG expects contiguous C-style arrays for input
dummy_img_array = np.zeros((height, width, 3), dtype=np.uint8)
# Set a pixel to demonstrate it's not entirely blank, just to ensure data is there
dummy_img_array[50, 50] = [255, 0, 0] # Blue pixel

# Encode the NumPy array (its byte representation) to JPEG
# TJPF_BGR indicates the input pixel format
try:
    jpeg_data = jpeg.encode(dummy_img_array.tobytes(), width, height, TJPF_BGR, quality=85)
    print(f"Successfully encoded JPEG data. Size: {len(jpeg_data)} bytes")

    # Save to a file for verification (optional)
    # with open('output.jpg', 'wb') as f:
    #     f.write(jpeg_data)

    # --- Decode Example ---
    # Decode the JPEG data back to raw pixel data
    # TJ_BGR indicates the desired output colorspace (constant for BGR output)
    decoded_image_data, decoded_width, decoded_height, decoded_pix_fmt, decoded_colorspace = \
        jpeg.decode(jpeg_data, TJ_BGR)

    print(f"Decoded image: {decoded_width}x{decoded_height}, "
          f"Pixel Format: {decoded_pix_fmt}, Colorspace: {decoded_colorspace}")

    # Convert decoded raw data back to a NumPy array
    decoded_array = np.frombuffer(decoded_image_data, dtype=np.uint8).reshape((decoded_height, decoded_width, 3))
    print(f"Decoded NumPy array shape: {decoded_array.shape}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure libjpeg-turbo is correctly installed and discoverable by PyTurboJPEG.")
    print("You might need to set LD_LIBRARY_PATH (Linux), DYLD_LIBRARY_PATH (macOS), or PATH (Windows).")
    print("Alternatively, explicitly pass the library path to TurboJPEG() constructor.")

view raw JSON →