Skip to main content

Image transformation, compression, and decompression codecs

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, 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, LZW, LZF, PGLZ (PostgreSQL LZ), RCOMP (Rice), ZFP, AEC, LERC, NPY, PNG, APNG, GIF, TIFF, WebP, QOI, JPEG 8-bit, JPEG 12-bit, Lossless JPEG (LJPEG, SOF3), JPEG 2000 (JP2, J2K), JPEG LS, JPEG XR (WDP, HD Photo), JPEG XL, MOZJPEG, AVIF, HEIF, Jetraw, PackBits, Packed Integers, Delta, XOR Delta, Floating Point Predictor, Bitorder reversal, Byteshuffle, Bitshuffle, CMS (color space transformations), and Float24 (24-bit floating point).

Author:

Christoph Gohlke

License:

BSD 3-Clause

Version:

2022.7.31

DOI:

10.5281/zenodo.6915978

Installation

Install the imagecodecs package from the Python Package Index:

python -m pip install -U imagecodecs

Wheels for macOS may not be available for the latest releases.

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

See Requirements and Notes for building from source.

Requirements

This release has been tested with the following requirements and dependencies (other versions may work):

Build requirements:

Test requirements:

Revisions

2022.7.31

  • Pass 6348 tests.

  • Add option to decode WebP as RGBA.

  • Add option to specify WebP compression method.

  • Use exact lossless WebP encoding.

2022.7.27

  • Add LZW encoder.

  • Add QOI codec via qoi.h (#37).

  • Add HEIF codec via libheif (source only; #33).

  • Add JETRAW codec via Jetraw demo (source only).

  • Add ByteShuffle codec, a generic version of FloatPred.

  • Replace imcd_floatpred by imcd_byteshuffle (breaking).

  • Use bool type in imcd (breaking).

2022.2.22

  • Fix jpeg numcodecs with tables (#28).

  • Add APNG codec via libpng-apng patch.

  • Add lossless and decodingspeed parameters to jpegxl_encode (#30).

  • Add option to read JPEG XL animations.

  • Add dummy numthreads parameter to codec functions.

  • Set default numthreads to 1 (disable multi-threading).

  • Drop support for Python 3.7 and numpy < 1.19 (NEP29).

2021.11.20

  • Fix testing on pypy and Python 3.10.

2021.11.11

  • Require libjxl 0.6.x.

  • Add CMS codec via Little CMS library for color space transformations (WIP).

  • Add MOZJPEG codec via mozjpeg library (Windows only).

  • Add SPNG codec via libspng library.

  • Rename avif_encode maxthreads parameter to numthreads (breaking).

  • Accept n-dimensional output in non-image numcodecs decoders.

  • Support masks in LERC codec.

  • Support multi-threading and planar format in JPEG2K codec.

  • Support multi-resolution, MCT, bitspersample, and 32-bit in jpeg2k encoder.

  • Change jpeg2k_encode level parameter to fixed quality psnr (breaking).

  • Change jpegxl_encode effort parameter default to minimum 3.

  • Change JPEG encoders to use YCbCr for RGB images by default.

  • Replace lerc_encode planarconfig with planar parameter (breaking).

  • Add option to specify omp numthreads and chunksize in ZFP codec.

  • Set default numthreads to 0.

  • Fix Blosc default typesize.

  • Fix segfault in jpegxl_encode.

  • Replace many constants with enums (breaking).

2021.8.26

  • Add BLOSC2 codec via c-blosc2 library.

  • Require LERC 3 and libjxl 0.5.

  • Do not exceed literal-only size in PackBits encoder.

  • Raise ImcdError if output is insufficient in PackBits codecs (breaking).

  • Raise ImcdError if input is corrupt in PackBits decoder (breaking).

  • Fix delta codec for non-native byteorder.

2021.7.30

Refer to the CHANGES file for older revisions.

Notes

This library is largely work in progress.

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

Works on little-endian platforms only.

The tiff, 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.

This software includes a modified version of dcm2niix’s jpg_0XC3.cpp.

This software includes a modified version of PostgreSQL’s pg_lzcompress.c.

This software includes a modified version of bitshuffle.

This software includes a modified version of liblj92.

This software includes qoi.h.

Build instructions and wheels for manylinux and macOS courtesy of Grzegorz Bokota.

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 libcfitsio-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, e.g.:

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

The apng, avif, jetraw, jpeg12, jpegls, jpegxl, lerc, lz4f, mozjpeg, 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, packbits, isa-l.igzip, fpzip, libmng, OpenEXR (EXR, PIZ, PXR24, B44, DWA), pyJetraw, tinyexr, pytinyexr, libjpeg (GPL), pylibjpeg, pylibjpeg-libjpeg (GPL), pylibjpeg-openjpeg, pylibjpeg-rle, glymur, pyheif, pyrus-cramjam, QuickLZ (GPL), LZO (GPL), nvJPEG, nvJPEG2K, PyTurboJPEG, CCSDS123, LPC-Rice, MAFISC.

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:

>>> bool(JPEG2K)
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)
>>> 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

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

$ python -m imagecodecs _test.jp2

Project details


Download files

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

Source Distribution

imagecodecs-2022.7.31.tar.gz (11.9 MB view hashes)

Uploaded Source

Built Distributions

imagecodecs-2022.7.31-pp38-pypy38_pp73-win_amd64.whl (14.6 MB view hashes)

Uploaded PyPy Windows x86-64

imagecodecs-2022.7.31-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (37.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

imagecodecs-2022.7.31-cp311-cp311-win_amd64.whl (14.9 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

imagecodecs-2022.7.31-cp311-cp311-win32.whl (12.8 MB view hashes)

Uploaded CPython 3.11 Windows x86

imagecodecs-2022.7.31-cp310-cp310-win_amd64.whl (14.9 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

imagecodecs-2022.7.31-cp310-cp310-win32.whl (12.8 MB view hashes)

Uploaded CPython 3.10 Windows x86

imagecodecs-2022.7.31-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (33.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

imagecodecs-2022.7.31-cp310-cp310-macosx_10_9_x86_64.whl (12.7 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

imagecodecs-2022.7.31-cp39-cp39-win_amd64.whl (15.0 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

imagecodecs-2022.7.31-cp39-cp39-win32.whl (12.8 MB view hashes)

Uploaded CPython 3.9 Windows x86

imagecodecs-2022.7.31-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

imagecodecs-2022.7.31-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (31.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

imagecodecs-2022.7.31-cp39-cp39-macosx_10_9_x86_64.whl (12.7 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

imagecodecs-2022.7.31-cp38-cp38-win_amd64.whl (15.0 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

imagecodecs-2022.7.31-cp38-cp38-win32.whl (12.8 MB view hashes)

Uploaded CPython 3.8 Windows x86

imagecodecs-2022.7.31-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

imagecodecs-2022.7.31-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (31.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

imagecodecs-2022.7.31-cp38-cp38-macosx_10_9_x86_64.whl (12.6 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

Supported by

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