Skip to main content

Blosc2 grok

A plugin of the excellent grok library for Blosc2. grok is a JPEG2000 codec, and with this plugin, you can use it as yet another codec in applications using Blosc2. See an example of use at: https://github.com/Blosc/blosc2_grok/blob/main/examples/params.py

Installation

For using blosc2_grok you will first have to install its wheel:

pip install blosc2-grok -U

Usage

import blosc2
import numpy as np
import blosc2_grok
from PIL import Image

# Set the params for the grok codec
kwargs = {}
kwargs['cod_format'] = blosc2_grok.GrkFileFmt.GRK_FMT_JP2
kwargs['quality_mode'] = "dB"
kwargs['quality_layers'] = np.array([5], dtype=np.float64)
blosc2_grok.set_params_defaults(**kwargs)

# Define the compression and decompression parameters for Blosc2.
# Disable the filters and do not split blocks (these won't work with grok).
cparams = {
    'codec': blosc2.Codec.GROK,
    'filters': [],
    'splitmode': blosc2.SplitMode.NEVER_SPLIT,
}

# Read the image
im = Image.open("examples/kodim23.png")
# Convert the image to a numpy array
np_array = np.asarray(im)

# Transform the numpy array to a blosc2 array. This is where compression happens, and
# the HTJ2K codec is called.
bl_array = blosc2.asarray(
    np_array,
    chunks=np_array.shape,
    blocks=np_array.shape,
    cparams=cparams,
    urlpath="examples/kodim23.b2nd",
    mode="w",
)

# Print information about the array, see the compression ratio (cratio)
print(bl_array.info)

Parameters for compression

The following parameters are available for compression for grok, with their defaults. Most of them are named after the ones in the Pillow library and have the same meaning. The ones that are not in Pillow are marked with a * and you can get more information about them in the grok documentation, or by following the provided links. For those marked with a **, you can get more information in the grok.h header.

'tile_size': (0, 0),
'tile_offset': (0, 0),
'quality_mode': None,
'quality_layers': np.zeros(0, dtype=np.float64),
'progression': "LRCP",
'num_resolutions': 6,
'codeblock_size': (64, 64),
'irreversible': False,
'precinct_size': (0, 0),
'offset': (0, 0),
'mct': 0,
* 'numgbits': 2,  # Equivalent to -N, -guard_bits
* 'roi_compno': -1,  # Together with 'roi_shift' it is equivalent to -R, -ROI
* 'roi_shift': 0,
* 'decod_format': GrkFileFmt.GRK_FMT_UNK,
* 'cod_format': GrkFileFmt.GRK_FMT_UNK,
* 'rsiz': GrkProfile.GRK_PROFILE_NONE,  # Equivalent to -Z, -rsiz
* 'framerate': 0,
* 'apply_icc_': False,  # Equivalent to -f, -apply_icc
* 'rateControlAlgorithm': GrkRateControl.BISECT,
* 'num_threads': 0,
* 'deviceId': 0,  # Equivalent to -G, -device_id
* 'duration': 0,  # Equivalent to -J, -duration
* 'repeats': 1,  # Equivalent to -e, -repetitions
* 'mode': GrkMode.DEFAULT,  # Equivalent to -M, -mode
* 'verbose': False,  # Equivalent to -v, -verbose
** 'enableTilePartGeneration': False,  # See header of grok.h above
** 'max_cs_size': 0,  # See header of grok.h above
** 'max_comp_size': 0,  # See header of grok.h above

*Note: * when using the blosc2_grok plugin from C, the structure used for setting the parameters uses the grok parameters names. You can see an example in https://github.com/Blosc/leaps-examples/blob/main/c-compression/compress-tomo.c#L110 .

codec_meta as rates quality mode

As a simpler way to activate the rates quality mode, if you set the codec_meta from the cparams to an integer different from 0, the rates quality mode will be activated with a rate value equal to codec_meta / 10. If cod_format is not specified, the default will be used. The codec_meta has priority to the rates param set with the blosc2_grok.set_params_defaults(). Please note that only rates < 25.6 are supported with this notation.

import blosc2


cparams = {
    'codec': blosc2.Codec.GROK,
    'codec_meta': 5 * 10,  # cratio will be 5
    'filters': [],
    'splitmode': blosc2.SplitMode.NEVER_SPLIT,
}

Notes

When using blosc2_grok, there are some restrictions that you have to keep in mind.

  • The minimum supported image size is around 256 bytes, so an image with less size will fail to be compressed.
  • The maximum datatype precision is of 16 bits.
  • Although floats from 16 or fewer bits of precision seem to work, we recommend using integer data when possible.

More examples

See the examples directory for more examples.

Thanks

Thanks to Marta Iborra, from the Blosc Development Team, for doing most of the job in making this plugin possible, and J. David Ibáñez and Francesc Alted for the initial contributions. Also, thanks to Aaron Boxer, the original author of the grok library, for his help in ironing out issues for making this interaction possible.

That's all folks!

The Blosc Development Team

Metadata

Release files for blosc2-grok 0.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for blosc2-grok 0.3.5
File
blosc2_grok-0.3.5-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
blosc2_grok-0.3.5-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
blosc2_grok-0.3.5-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64, Linux glibc 2.27+ ARM64 Details
blosc2_grok-0.3.5-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details

Total release size: 9.6 MB

Release files / blosc2_grok-0.3.5-py3-none-win_amd64.whl

Download URL blosc2_grok-0.3.5-py3-none-win_amd64.whl
Size 3.9 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
03a6856c2e42f6ee8f7e0161e22756ad37302093f47c4e80175a255a37e2379f
BLAKE2b-256 checksum
How to use checksums
92bf7787cbec21922338c0030174010260a2d9bc8a13f8fb00bbdd071f96f23c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / blosc2_grok-0.3.5-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL blosc2_grok-0.3.5-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 2.1 MB
Tags Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
6bc0f4895677246b2eebf7c16628dbfe7c67760045b22afbc5d6c8c5a4c9e7a2
BLAKE2b-256 checksum
How to use checksums
9f20c192886cff0cda0ac6428871c4e49d059f7c7a9a687d25b6986f72bde53f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / blosc2_grok-0.3.5-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl

Download URL blosc2_grok-0.3.5-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Size 2.0 MB
Tags Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
8404764f06292a3c211ceeaf684da8a3a89905f1db2c849dc15170bb8fe015ca
BLAKE2b-256 checksum
How to use checksums
8251b84a322e615d7723643e7b033db9de39ece1ff9ffd5af5231b2ee4cbff02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / blosc2_grok-0.3.5-py3-none-macosx_11_0_arm64.whl

Download URL blosc2_grok-0.3.5-py3-none-macosx_11_0_arm64.whl
Size 1.6 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5d6b6958c8588f717c7d38e3b7a2a3733494680ff5aa448def264d0a2decbfa3
BLAKE2b-256 checksum
How to use checksums
bc078993c5c53aa386c973438861d2e9b32d6da80f83f1d055b11acbe37e6217
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.3.5 This release

4 release files

0.3.4

5 release files

0.3.3

5 release files

0.3.2

5 release files

0.3.1

5 release files

0.3.0

4 release files

0.2.3

4 release files

0.2.2

4 release files

0.2.1

4 release files

0.2.0

4 release files

0.1.0

4 release files

0.0.1

4 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page