Skip to main content

Grok (JPEG2000 codec) plugin for Blosc2.

Project description

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

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

blosc2_grok-0.3.4-py3-none-win_amd64.whl (3.9 MB view details)

Uploaded Python 3Windows x86-64

blosc2_grok-0.3.4-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded Python 3manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

blosc2_grok-0.3.4-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (2.0 MB view details)

Uploaded Python 3manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

blosc2_grok-0.3.4-py3-none-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

blosc2_grok-0.3.4-cp312-cp312-win_amd64.whl (3.9 MB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file blosc2_grok-0.3.4-py3-none-win_amd64.whl.

File metadata

  • Download URL: blosc2_grok-0.3.4-py3-none-win_amd64.whl
  • Upload date:
  • Size: 3.9 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for blosc2_grok-0.3.4-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 85a6c22da5e08a7d24749cd59397e70e4de02636be08e9438fb27956b769115b
MD5 5a7f0fea020a75ea2a060bc9bcfdd5f9
BLAKE2b-256 5ac2733b8fc886e11e6f4072e28a0aa9febee08a4e62833dbbf9064707061599

See more details on using hashes here.

File details

Details for the file blosc2_grok-0.3.4-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for blosc2_grok-0.3.4-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bf50f629a9fe341b658cd96242b1d7f8f361e46c08c91f156db107f4d2cb67c3
MD5 ed370a8c86bef040d7974da7a07ce530
BLAKE2b-256 3de307fdaa24603db0144e745f9646e2eb1189e4e801b35e18b6685818513480

See more details on using hashes here.

File details

Details for the file blosc2_grok-0.3.4-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for blosc2_grok-0.3.4-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c93a2685e9b4a7dcdf95549c14c23bc29bd6d7604a5b978f6a4818d1f49b84f0
MD5 76845a1f78da161906f27ecd34f20c2d
BLAKE2b-256 5d61a1a59e7f71baf8d42c4665234e6b42d37e7089134c902b7b64ef3c0fb300

See more details on using hashes here.

File details

Details for the file blosc2_grok-0.3.4-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for blosc2_grok-0.3.4-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0d2a4675c546219ac49b194c4a2a6310906cb2c0bb243ad7010589bc8ababfae
MD5 f452f709d66a7bc299b41ed0c14d9d0d
BLAKE2b-256 0197dd4a2cc899fb49b60898b27754cc05526e5272da9da86fdcd3e24ec89da0

See more details on using hashes here.

File details

Details for the file blosc2_grok-0.3.4-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for blosc2_grok-0.3.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 78095b887a8291d76be7fa80cc58f8e077c0c9be8b9bd005584aa9464edf4c4a
MD5 ba514c87fd66a07365674b18fa6a804e
BLAKE2b-256 d33526b4d534df19f60b4861a8714cfb8be28ccd5f9cb8a92c572e7d46bb8584

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