simplejpeg

1.9.0 · active · verified Thu Apr 16

simplejpeg is a Python library offering fast JPEG encoding and decoding, built upon recent versions of libturbojpeg. It targets use cases prioritizing speed and direct memory access over broader image format support. The library is actively maintained with regular releases, currently at version 1.9.0, providing efficient handling of JPEG images as NumPy arrays.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates encoding a NumPy array representing an RGB image into JPEG bytes, and then decoding those bytes back into a NumPy array using simplejpeg. It also includes a check for JPEG header information.

import numpy as np
from simplejpeg import encode_jpeg, decode_jpeg

# 1. Create a dummy RGB image (NumPy array)
width, height = 640, 480
image_data_rgb = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)

# 2. Encode the image to JPEG bytes
jpeg_bytes = encode_jpeg(image_data_rgb, quality=85, colorspace='RGB')
print(f"Encoded JPEG size: {len(jpeg_bytes)} bytes")

# 3. Decode the JPEG bytes back to a NumPy array
decoded_image_rgb = decode_jpeg(jpeg_bytes, colorspace='RGB')
print(f"Decoded image shape: {decided_image_rgb.shape}")

# Optional: Check if the data is a JPEG
is_it_jpeg = decode_jpeg_header(jpeg_bytes)
print(f"Is the data a JPEG? {'Yes' if is_it_jpeg else 'No'}")

view raw JSON →