Image transformation, compression, and decompression codecs

2026.3.6 · active · verified Sat Apr 11

Imagecodecs is a Python library that provides block-oriented, in-memory buffer transformation, compression, and decompression functions for various image and scientific data formats. It wraps numerous C libraries to support a wide array of codecs, including Zlib, JPEG, PNG, WebP, AVIF, and many others. It is actively maintained with frequent releases, currently at version 2026.3.6, and primarily depends on NumPy.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates encoding a NumPy array representing an image into a PNG byte string and then decoding it back. It verifies that the original and decoded images are identical.

import numpy as np
from imagecodecs import png_encode, png_decode

# Create a dummy image (e.g., 100x100 grayscale image)
image_data = np.arange(100 * 100, dtype=np.uint8).reshape((100, 100))

# Encode the image to PNG format (bytes)
encoded_data = png_encode(image_data)
print(f"Encoded data size: {len(encoded_data)} bytes")

# Decode the PNG data back to a NumPy array
decoded_image = png_decode(encoded_data)
print(f"Decoded image shape: {decoded_image.shape}, dtype: {decoded_image.dtype}")

# Verify data integrity
assert np.array_equal(image_data, decoded_image)
print("Image encoded and decoded successfully!")

view raw JSON →