Image transformation, compression, and decompression codecs
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
- breaking The API is explicitly stated as not stable and may change between revisions. Users should expect breaking changes and consult release notes before upgrading, especially for major versions.
- breaking Recent versions (e.g., 2026.3.6) enforce positional-only and keyword-only parameters for many functions. This can break older code that relies on keyword arguments where positional-only is now required.
- breaking Support for older Python versions is frequently dropped. For example, Python <= 3.10 was dropped in 2025.8.2, and Python 3.11 was deprecated in 2025.11.11.
- breaking Many codec-specific parameters have been renamed or had their behavior altered across versions (e.g., `jpegxl_encode` level, `avif_encode` maxthreads/numthreads, `lerc_encode` planarconfig/planar). Additionally, many constants have been replaced by enums.
- gotcha Building `imagecodecs` from source can be challenging due to its numerous external C library dependencies. Pre-built wheels are highly recommended for most users. Some codecs might be decode-only or unavailable on certain platforms/architectures.
Install
-
pip install -U "imagecodecs[all]" -
pip install -U imagecodecs
Imports
- jpeg_encode
from imagecodecs import jpeg_encode
- png_decode
from imagecodecs import png_decode
- imagecodecs
import imagecodecs
- Jpeg2k
from imagecodecs.numcodecs import Jpeg2k
Quickstart
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!")