Skip to main content

Image transformation, compression, and decompression codecs

Reason this release was yanked:

Need space

Project description

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

Decode and/or encode functions are implemented for Zlib (DEFLATE), GZIP, ZStandard (ZSTD), Blosc, Brotli, Snappy, LZMA, BZ2, LZ4, LZ4F, LZ4HC, LZ4H5, LZW, LZO, LZF, LZFSE, LZHAM, PGLZ (PostgreSQL LZ), RCOMP (Rice), ZFP, SPERR, AEC, SZIP, LERC, EER, NPY, BCn, DDS, BMP, PNG, APNG, GIF, TIFF, WebP, QOI, JPEG 8 and 12-bit, Lossless JPEG (LJPEG, LJ92, JPEGLL), JPEG 2000 (JP2, J2K), JPEG LS, JPEG XL, JPEG XR (WDP, HD Photo), MOZJPEG, AVIF, HEIF, 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:

2024.1.1

DOI:

10.5281/zenodo.6915978

Quickstart

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

python -m pip install -U imagecodecs[all]

Imagecodecs is also available in other package repositories such as Anaconda, MSYS2, and MacPorts.

See Requirements and Notes for building from source.

See Examples for using the programming interface.

Source code and support are available on GitHub.

Requirements

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

  • CPython 3.9.13, 3.10.11, 3.11.7, 3.12.1, 64-bit

  • Numpy 1.26.2

  • numcodecs 0.12.1 (optional, for Zarr compatible codecs)

Build requirements:

Vendored requirements:

Test requirements:

Revisions

2024.1.1

  • Pass 7420 tests.

  • Add 8/24-bit BMP codec.

  • Add SPERR codec based on SPERR library.

  • Add LZO decoder based on lzokay library.

  • Add DICOMRLE decoder.

  • Enable float16 in CMS codec.

  • Enable MCT for lossless JPEG2K encoder (#88).

  • Ignore pad-byte in PackBits decoder (#86).

  • Fix heif_write_callback error message not set.

  • Require lcms2 2.16 with issue-420 fixes.

  • Require libjxl 0.9, libaec 1.1, Cython 3.

2023.9.18

  • Rebuild with updated dependencies fixes CVE-2024-4863.

2023.9.4

  • Map avif_encode level parameter to quality (breaking).

  • Support monochrome images in avif_encode.

  • Add numthreads parameter to avif_decode (fix imread of AVIF).

  • Add quantize filter (BitGroom, BitRound, GBR) via nc4var.c.

  • Add LZ4H5 codec.

  • Support more BCn compressed DDS fourcc types.

  • Require libavif 1.0.

2023.8.12

  • Add EER (Electron Event Representation) decoder.

  • Add option to pass initial value to crc32 and adler32 checksum functions.

  • Add fletcher32 and lookup3 checksum functions via HDF5’s h5checksum.c.

  • Add Checksum codec for numcodecs.

2023.7.10

  • Rebuild with optimized compile flags.

2023.7.4

  • Add BCn and DDS decoder via bcdec library.

  • Add functions to transcode JPEG XL to/from JPEG (#78).

  • Add option to decode select frames from animated WebP.

  • Use legacy JPEG8 codec when building without libjpeg-turbo 3 (#65).

  • Change blosc2_encode defaults to match blosc2-python (breaking).

  • Fix segfault writing JPEG2K with more than 4 samples.

  • Fix some codecs returning bytearray by default.

  • Fully vendor cfitsio’s ricecomp.c.

  • Drop support for Python 3.8 and numpy < 1.21 (NEP29).

2023.3.16

  • Require libjpeg-turbo 2.1.91 (3.0 beta) and c-blosc2 2.7.1.

  • Add experimental type hints.

  • Add SZIP codec via libaec library.

  • Use Zstd streaming API to decode blocks with unknown decompressed size.

  • Remove unused level, index, and numthreads parameters (breaking).

  • Make AEC and BLOSC constants enums (breaking).

  • Capitalize numcodecs class names (breaking).

  • Remove JPEG12 codec (breaking; use JPEG8 instead).

  • Encode and decode lossless and 12-bit JPEG with JPEG8 codec by default.

  • Remove JPEGSOF3 fallback in JPEG codec.

  • Fix slow IFD seeking with libtiff 4.5.

  • Fixes for Cython 3.0.

2023.1.23

  • Require libjxl 0.8.

  • Change mapping of level to distance parameter in jpegxl_encode.

  • Add option to specify bitspersample in jpegxl_encode.

  • Add option to pass de/linearize tables to LJPEG codec.

  • Fix lj92 decoder for SSSS=16 (#59).

  • Prefer ljpeg over jpegsof3 codec.

  • Add option to specify AVIF encoder codec.

  • Support LERC with Zstd or Deflate compression.

  • Squeeze chunk arrays by default in numcodecs image compression codecs.

2022.12.24

Refer to the CHANGES file for older revisions.

Objectives

Many scientific image storage formats like TIFF, CZI, DICOM, HDF, and Zarr are containers that hold large numbers of small data segments (chunks, tiles, stripes), which are encoded using a variety of compression and pre-filtering methods. Metadata common to all data segments are typically stored separate 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:

  • Provide functions for encoding and decoding small image data segments in-memory (not in-file) from and to bytes or numpy arrays for many compression and filtering methods.

  • Support image formats and compression methods not available elsewhere in the Python ecosystem.

  • Reduce the runtime dependency on numerous, large, inapt, or unmaintained Python packages. The imagecodecs package only depends on numpy.

  • Implement codecs as Cython wrappers of 3rd party libraries with a C API and permissive license if exists, else use own C library. Provide Cython definition files for the wrapped C libraries.

  • Release the Python global interpreter lock (GIL) during extended native/C function calls for multi-threaded use.

Accessing parts of large data segments and reading metadata from segments are out of 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.8 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, and manylinux_x86_64.

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 and jetraw 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-2022 is 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

Install the requirements for building imagecodecs from source code on latest Ubuntu Linux distributions:

sudo apt-get install build-essential python3-dev cython3 python3-setuptools python3-pip python3-wheel python3-numpy python3-zarr python3-pytest python3-blosc python3-brotli python3-snappy python3-lz4 libz-dev libblosc-dev liblzma-dev liblz4-dev libzstd-dev libpng-dev libwebp-dev libbz2-dev libopenjp2-7-dev libjpeg-dev libjxr-dev liblcms2-dev libcharls-dev libaec-dev libbrotli-dev libsnappy-dev libzopfli-dev libgif-dev libtiff-dev libdeflate-dev libavif-dev libheif-dev

Use the --lite build option to only build extensions without 3rd-party dependencies. Use the --skip-extension build options to skip building specific extensions, for example:

python -m pip install imagecodecs --global-option="build_ext" --global-option="--skip-bitshuffle"

The apng, avif, jetraw, jpegls, jpegxl, lerc, lz4f, lzfse, lzham, lzo, mozjpeg, sperr, zfp, and zlibng extensions are disabled by default when building from source.

To modify other build settings such as library names and compiler arguments, provide a imagecodecs_distributor_setup.customize_build function, which is imported and executed during setup. See setup.py for examples.

Other Python packages and C libraries 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.

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.0'

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 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.core.Array (4, 5, 512, 512, 3) uint8>

Access image data in a sequence of JP2 files via tifffile.FileSequence and dask.array:

>>> 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 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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

imagecodecs-2024.1.1.tar.gz (9.5 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

imagecodecs-2024.1.1-1-cp310-cp310-win_amd64.whl (25.4 MB view details)

Uploaded CPython 3.10Windows x86-64

imagecodecs-2024.1.1-1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (39.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

imagecodecs-2024.1.1-1-cp310-cp310-macosx_11_0_arm64.whl (12.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

imagecodecs-2024.1.1-1-cp39-cp39-win_amd64.whl (25.4 MB view details)

Uploaded CPython 3.9Windows x86-64

imagecodecs-2024.1.1-1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (39.7 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

imagecodecs-2024.1.1-1-cp39-cp39-macosx_11_0_arm64.whl (12.5 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

imagecodecs-2024.1.1-1-cp39-cp39-macosx_10_14_x86_64.whl (15.2 MB view details)

Uploaded CPython 3.9macOS 10.14+ x86-64

File details

Details for the file imagecodecs-2024.1.1.tar.gz.

File metadata

  • Download URL: imagecodecs-2024.1.1.tar.gz
  • Upload date:
  • Size: 9.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for imagecodecs-2024.1.1.tar.gz
Algorithm Hash digest
SHA256 fde46bd698d008255deef5411c59b35c0e875295e835bf6079f7e2ab22f216eb
MD5 0509b94cf074c791e7de6acd084bd738
BLAKE2b-256 92e3e17c8b9703dc467041ac76d41fe18f67d877ea0ab35d8fac2046e3873945

See more details on using hashes here.

File details

Details for the file imagecodecs-2024.1.1-1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for imagecodecs-2024.1.1-1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 043d28cb0815e72832f1bbe35950f02c455d91d4c5dee9f2fa5a8f72363c7e13
MD5 c35caf10deb49265761f61ad65816f78
BLAKE2b-256 a37e249a1e255ca92b4a21ae4eead6332688e9bd77135b159684c6eddc9c20c1

See more details on using hashes here.

File details

Details for the file imagecodecs-2024.1.1-1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for imagecodecs-2024.1.1-1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 07575d0fd904f59fe7c19973b0e81520175d9ecec7aa26e0f55eddd0458cdd9c
MD5 69920c1e4973c862d0e6da4d0c8f88bd
BLAKE2b-256 e8ec9eca05890c82170de9fdf2aa2278595e7f6f50b4423de3f1519ca9a3ed13

See more details on using hashes here.

File details

Details for the file imagecodecs-2024.1.1-1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for imagecodecs-2024.1.1-1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d5a4dec3c7b4014c9b0d0cc8638c2aefa1545a8d8b31dacfebaabe0e664fb37
MD5 6b25055cf7adad9caeacc2c4226444d5
BLAKE2b-256 ed1bb0f0c2f6a736fbd84b85d0f2a028d5c89404d064bdeb93bd3b57540cda14

See more details on using hashes here.

File details

Details for the file imagecodecs-2024.1.1-1-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for imagecodecs-2024.1.1-1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 525bf16a2df626b31c887f0d46023270feeb2be8d9ff6d3ca33666786686b8c7
MD5 1e8223faac41de5b21b9244b09e0e0f7
BLAKE2b-256 9c56e0b2e94d8c901d856ba2d92518a34079efeb121df10980c5c2544cf6aa33

See more details on using hashes here.

File details

Details for the file imagecodecs-2024.1.1-1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for imagecodecs-2024.1.1-1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3ff3d2b27ca7d388b200073b201de522ece8c805f99b1fa5f97c739051f896f1
MD5 4203045168cbeca251f3bdcf399decb8
BLAKE2b-256 873eae58588157fa86c9c3970e6dc8ab4e71c946c9180070c35f854e7a6f8112

See more details on using hashes here.

File details

Details for the file imagecodecs-2024.1.1-1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for imagecodecs-2024.1.1-1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 57ac95505df57f210735e3870d5b007063a7ff3f91a37babef1f0491c68f3a95
MD5 cc6e2b676c1c798a30ca20e2fb3ea9fa
BLAKE2b-256 8ff952d3fbd5049f43c7a13951610abe45f88ba1b638f4798e2a690cc09c25ef

See more details on using hashes here.

File details

Details for the file imagecodecs-2024.1.1-1-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for imagecodecs-2024.1.1-1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 937f18d75b630f6a2398d6671324668df006bb53c89aeaec31a67db78930938e
MD5 27f97b33f41c9552463c2c218d4d1304
BLAKE2b-256 b428f44e76c07ef344295f812ad5dd3ed4e358d70d0f42cbedfcbf5de330f71f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page