GitHub - cgohlke/imagecodecs: Image transformation, compression, and decompression codecs. (original) (raw)

Image transformation, compression, and decompression codecs

Imagecodecs is a Python library that provides block-oriented, in-memory buffer transformation, compression, and decompression functions for use in Tifffile, Liffile, Czifile, Zarr 2, kerchunk, and other scientific image input/output packages.

Decode and/or encode functions are implemented for Zlib (DEFLATE), GZIP, LZMA, ZStandard (ZSTD), Blosc, Brotli, Snappy, BZ2, LZ4, LZ4F, LZ4HC, LZ4H5, LZW, LZO, LZF, LZFSE, LZHAM, PGLZ (PostgreSQL LZ), RCOMP (Rice), ZFP, SZ3, Pcodec, SPERR, AEC, SZIP, LERC, EER, NPY, BCn, DDS, BMP, PNG, APNG, GIF, TIFF, WebP, JPEG (2 to 16-bit), Lossless JPEG (LJPEG, LJ92, JPEGLL), JPEG 2000 (JP2, J2K), JPEG LS, JPEG XL, JPEG XS, JPEG XR (WDP, HD Photo), Ultra HDR (JPEG_R), MOZJPEG, AVIF, HEIF, QOI, RGBE (HDR), Jetraw, DICOMRLE, PackBits, Packed Integers, Delta, XOR Delta, Floating Point Predictor, Bitorder reversal, Byteshuffle, Bitshuffle, Float24 (24-bit floating point), Quantize (Scale, BitGroom, BitRound, GranularBR), and CMS (color space transformations). Checksum functions are implemented for crc32, adler32, fletcher32, and Jenkins lookup3.

Author: Christoph Gohlke
License: BSD 3-Clause
Version: 2025.3.30
DOI: 10.5281/zenodo.6915978

Quickstart

Install the imagecodecs package and all dependencies from thePython Package Index:

python -m pip install -U "imagecodecs[all]"

Imagecodecs is also available in other package repositories such asAnaconda,MSYS2, andMacPorts.

See Requirements and Notes for building from source.

See Examples for using the programming interface.

Source code and support are available onGitHub.

Requirements

This revision was tested with the following requirements and dependencies (other versions may work):

Build requirements:

Vendored requirements:

Test requirements:

Revisions

2025.3.30

2024.12.30

2024.9.22

2024.6.1

2024.1.1

2023.9.18

Refer to the CHANGES file for older revisions.

Objectives

Many scientific image storage formats, such as TIFF, CZI, XLIF, DICOM, HDF, and Zarr are containers that store numerous small data segments (chunks, tiles, stripes). These segments are encoded using various compression and pre-filtering methods. Metadata common to all data segments are typically stored separately from the segments.

The purpose of the Imagecodecs library is to support Python modules in encoding and decoding such data segments. The specific aims are:

Accessing parts of large data segments and reading metadata from segments are outside the scope of this library.

Notes

This library is largely a work in progress.

The API is not stable yet and might change between revisions.

Python <= 3.9 is no longer supported. 32-bit versions are deprecated.

Works on little-endian platforms only.

Supported platforms are win_amd64, win_arm64, win32,macosx_x86_64, macosx_arm64, manylinux_x86_64, andmanylinux_aarch64.

Wheels may not be available for all platforms and all releases.

Only the win_amd64 wheels include all features.

The tiff, bcn, dds, dicomrle, eer, lzo, packints, and jpegsof3 codecs are currently decode-only.

The heif, jetraw, and jpegxs codecs are distributed as source code only due to license and possible patent usage issues.

The latest Microsoft Visual C++ Redistributable for Visual Studio 2015-2022is required on Windows.

Refer to the imagecodecs/licenses folder for 3rd-party library licenses.

This software is based in part on the work of the Independent JPEG Group.

Update pip and setuptools to the latest version before installing imagecodecs:

python -m pip install -U pip setuptools wheel Cython

Before building imagecodecs from source code, install required tools and libraries. For example, on latest Ubuntu Linux distributions:

sudo apt-get install build-essential python3-dev cython3 python3-pip python3-setuptools python3-wheel python3-numpy libdeflate-dev libjpeg-dev libjxr-dev liblcms2-dev liblz4-dev liblerc-dev liblzma-dev libopenjp2-7-dev libpng-dev libtiff-dev libwebp-dev libz-dev libzstd-dev

To build and install imagecodecs from source code, run:

python -m pip install .

Many extensions are disabled by default when building from source.

To define which extensions are built, or to modify build settings such as library names and compiler arguments, provide aimagecodecs_distributor_setup.customize_build function, which is imported and executed during setup. See setup.py for pre-defined customize_build functions.

Other projects providing imaging or compression codecs:Python zlib,Python bz2,Python lzma,backports.lzma,python-lzo,python-lzw,python-lerc,wavpack-numcodecs,packbits,isa-l.igzip,fpzip,libmng,OpenEXR(EXR, PIZ, PXR24, B44, DWA),pyJetraw,tinyexr,pytinyexr,pyroexr,JasPer,libjpeg (GPL),pylibjpeg,pylibjpeg-libjpeg (GPL),pylibjpeg-openjpeg,pylibjpeg-rle,glymur,pyheif,pyrus-cramjam,PyLZHAM,BriefLZ,QuickLZ (GPL),LZO (GPL),nvJPEG,nvJPEG2K,PyTurboJPEG,CCSDS123,LPC-Rice,CompressionAlgorithms,Compressonator,Wuffs,TinyDNG,OpenJPH,Grok (AGPL),MAFISC,B3D,fo-dicom.Codecs,jpegli,hdf5plugin.

Examples

Import the JPEG2K codec:

from imagecodecs import ( ... jpeg2k_encode, ... jpeg2k_decode, ... jpeg2k_check, ... jpeg2k_version, ... JPEG2K, ... )

Check that the JPEG2K codec is available in the imagecodecs build:

JPEG2K.available True

Print the version of the JPEG2K codec's underlying OpenJPEG library:

jpeg2k_version() 'openjpeg 2.5.3'

Encode a numpy array in lossless JP2 format:

array = numpy.random.randint(100, 200, (256, 256, 3), numpy.uint8) encoded = jpeg2k_encode(array, level=0) bytes(encoded[:12]) b'\x00\x00\x00\x0cjP \r\n\x87\n'

Check that the encoded bytes likely contain a JPEG 2000 stream:

jpeg2k_check(encoded) True

Decode the JP2 encoded bytes to a numpy array:

decoded = jpeg2k_decode(encoded) numpy.array_equal(decoded, array) True

Decode the JP2 encoded bytes to an existing numpy array:

out = numpy.empty_like(array) _ = jpeg2k_decode(encoded, out=out) numpy.array_equal(out, array) True

Not all codecs are fully implemented, raising exceptions at runtime:

from imagecodecs import tiff_encode tiff_encode(array) Traceback (most recent call last): ... NotImplementedError: tiff_encode

Write the numpy array to a JP2 file:

from imagecodecs import imwrite, imread imwrite('_test.jp2', array)

Read the image from the JP2 file as numpy array:

image = imread('_test.jp2') numpy.array_equal(image, array) True

Create a JPEG 2000 compressed Zarr 2 array:

import zarr import numcodecs from imagecodecs.numcodecs import Jpeg2k numcodecs.register_codec(Jpeg2k) zarr.zeros( ... (4, 5, 512, 512, 3), ... chunks=(1, 1, 256, 256, 3), ... dtype='u1', ... compressor=Jpeg2k(), ... zarr_format=2, ... ) <...Array ...(4, 5, 512, 512, 3) ...uint8>

Access image data in a sequence of JP2 files via tifffile.FileSequence and dask.array (requires Zarr 2):

import tifffile import dask.array def jp2_read(filename): ... with open(filename, 'rb') as fh: ... data = fh.read() ... return jpeg2k_decode(data) ... with tifffile.FileSequence(jp2_read, '*.jp2') as ims: ... with ims.aszarr() as store: ... dask.array.from_zarr(store) ... dask.array<from-zarr, shape=(1, 256, 256, 3)...chunksize=(1, 256, 256, 3)...

Write the Zarr 2 store to a fsspec ReferenceFileSystem in JSON format and open it as a Zarr array:

store.write_fsspec( ... 'temp.json', url='file://', codec_id='imagecodecs_jpeg2k' ... ) import fsspec mapper = fsspec.get_mapper( ... 'reference://', fo='temp.json', target_protocol='file' ... ) zarr.open(mapper, mode='r') <zarr.core.Array (1, 256, 256, 3) uint8 read-only>

View the image in the JP2 file from the command line:

python -m imagecodecs _test.jp2