Plugin of the OpenHTJ2K codec for the C-Blosc2 library
Project description
Blosc2 OpenHTJ2K
This is a dynamic codec plugin for Blosc2 that allows to compress and decompress images using the High Throughput JPEG 2000 standard. The HT version brings increased performance when compared to the traditional JPEG 2000. For details, check the HTJ2K whitepaper.
To provide this feature this plugin uses the OpenHTJ2K library.
Install
The Blosc2 OpenHTJ2K plugin is distributed as a Python wheel:
pip install blosc2-openhtj2k
There are wheels for Linux, macOS and Windows. As of now, only the x86-64 architecture is supported.
Usage from Python
The examples are not distributed with the wheel, but you can just clone the project:
git clone https://github.com/Blosc/blosc2_openhtj2k.git
cd blosc2_openhtj2k
In the examples folder there are the compress and decompress scripts, both take two required arguments, for the input and output file:
-
compress.py
takes as first argument the path to an image, and the second argument the path to the output file, which should end by the.b2nd
extension. -
decompress.py
takes as first argument the path to the Blosc2 file generated bycompress.py
, and second argument the path to the output image.
To try out these scripts first install the required software:
pip install blosc2-openhtj2k
pip install Pillow
Then you can run the scripts, from the examples folder, for example:
cd examples
python compress.py kodim23.png /tmp/kodim23.b2nd
python decompress.py /tmp/kodim23.b2nd /tmp/kodim23.png
Note that the examples cannot be run from the project's root, because it will fail to
import blosc2_openhtj2k
, since there's a directory with that name.
For details on the arguments these commands accept call them with the --help
option.
Below follows more detailed docs on how to accomplish the compression and decompression.
Compression
Load the image
To compress an image first we need to load it, and to transform it to a Numpy array, then Blosc2 will compress that array.
For loading the image, and getting the Numpy array, we are going to use the Pillow library:
from PIL import Image
im = Image.open(args.inputfile)
np_array = np.asarray(im)
Transform the Numpy array
Before feeding this array to Blosc2, we need to massage it a bit, because its structure
is different from what is expected by the OpenHTJ2K plugin. As can be seen in the
compress.py
script, these are the transformations required, with comments:
# Transpose the array so the channel (color) comes first
# Change from (height, width, channel) to (channel, width, height)
np_array = np.transpose(np_array, (2, 1, 0))
# Make the array C-contiguous
np_array = np_array.copy()
# The library expects 4 bytes per color (xx 00 00 00), so change the type
np_array = np_array.astype('uint32')
Plugin options
It's possible to configure the OpenHTJ2K plugin with a number of options, this step is optional. For example:
import blosc2_openhtj2k
blosc2_openhtj2k.set_params_defaults(
transformation=0, # 0:lossy 1:lossless (default is 1)
)
Once the options above are set, these remain for all future calls, until they are changed again.
Blosc2 parameters
Note that:
-
We must tell Blosc2 to use the OpenHTJ2K codec, passing its corresponding id
BLOSC_CODEC_OPENHTJ2K
. -
At this time the plugin does not support multithreading, so the number of threads must be explicitly defined to 1.
-
OpenHTJ2K expects to work with images, so this plugin won't work well when combined with regular Blosc2 filters (
SHUFFLE
,BITSHUFFLE
,BYTEDELTA
...), nor with split mode, because they change the image completely; so these must be reset.
With that, we typically define the compression and decompression parameters like this:
nthreads = 1
cparams = {
'codec': blosc2.Codec.OPENHTJ2K,
'nthreads': nthreads,
'filters': [],
'splitmode': blosc2.SplitMode.NEVER_SPLIT,
}
dparams = {'nthreads': nthreads}
Actual OpenHTJ2K compression
Now we can call the command that will compress the image using Blosc2 and the OpenHTJ2K plugin:
bl_array = blosc2.asarray(
np_array,
chunks=np_array.shape,
blocks=np_array.shape,
cparams=cparams,
dparams=dparams,
)
Note that:
-
We set the chunk and block shape to match the image size, as this is well tested.
-
If you pass the
urlpath
the Blosc2 array will be saved to the given path, for example `urlpath=/tmp/image.b2nd'.
Decompression
If the Blosc2 array was saved to a file with a different program, we will need to read it first:
array = blosc2.open(args.inputfile)
Now decompressing it is easy, we will get a Numpy array:
np_array = array[:]
But prior to obtain the image, we must undo the transformations done when compressing:
# Get back 1 byte per color, change dtype from uint32 to uint8
np_array = np_array.astype('uint8')
# Get back the original shape: height, width, channel
np_array = np.transpose(np_array, (2, 1, 0))
Now we can get the Pillow image from the Numpy array:
im = Image.fromarray(np_array)
Which can be saved or displayed:
im.save(...)
im.show()
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 Distributions
Built Distributions
File details
Details for the file blosc2_openhtj2k-0.1.2-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 160.3 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c00adf23b6c21f01925f0d067ccf13666a651eeced9ba2af47cb28f2f903869f |
|
MD5 | b992d94e9b8fa4449dde08608b8aaa5f |
|
BLAKE2b-256 | 37118994b719bedad791297681f4b5ed7c2a1b0daea11809ad9fd260a2bb721b |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 358.8 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5a5271258518cb3b4ca4371a5ac2631c1114cefe02536f5adb41b4b72f365c6 |
|
MD5 | aaa0075dd44d5bd3dd5fc3bfcb938f1d |
|
BLAKE2b-256 | cd32f0f60b86aa01bd8543dd0ac00ae271090433b46bd09281b9d3d3a44deed0 |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp311-cp311-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp311-cp311-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.11, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46f1f8a01a2d85e07ef658a183a80f42308004f40630c21a5941c68cc7af03c3 |
|
MD5 | c4258f8748bede9d979db5c8a639a0a7 |
|
BLAKE2b-256 | d6d155d63043753ca350c9e9ec5cd7ec231ea6d07a8bb46446334ef366835749 |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 160.3 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e7ce77e8766f363884713b50d829d55517c75332cb9d1c9b17edc92f88d7e75 |
|
MD5 | 01aba6870ca545333f0a424503814b0a |
|
BLAKE2b-256 | d6564790bc1b58de1b3ba5c0120953cf98b893a6f388e87e82b76dac0bcd1603 |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 358.8 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d61a403e3dc114a9378366664003adfe9cb867ce451ad2a8d3dd0540d7b41fe9 |
|
MD5 | 667651735d99411ec6b452f563ac44f1 |
|
BLAKE2b-256 | eadf2f59c31700292f4d7d3c5a8825ccd648937cf1081d9233ebdb694e8a09bd |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp310-cp310-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp310-cp310-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.10, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3e47a0349b2ab566dc74dbe930d2c9cdad1def29cbdff27faa61b9aedfe69c3 |
|
MD5 | 31be749ef5e22720ca9cdb29bd649e01 |
|
BLAKE2b-256 | c8d2095fa01285d3411d075ae337119ee5e74eb5a398b406a3f1cbf63799e9c3 |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 160.3 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 237321c0d49e10d644698446d110ae28b5ca97c50bd873f277ee27354e82f72b |
|
MD5 | 3c3da8a70dc27c0f1ccdc09378ac5138 |
|
BLAKE2b-256 | 099b6f13e861db21f4336495055592b9cb52a6099faf04e22f8cba27a05b6afb |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 358.8 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 900a6e5617a0100db8885f492ecc17b0e3130c0e02120f63066c274a8f441448 |
|
MD5 | 567b52e05234eb8fcd7d5b2f9a6e3dfb |
|
BLAKE2b-256 | bd293f5b02463003059afa304a6d3a3cb5bf443e8f3c64fa3ed9b74eba58cf9b |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp39-cp39-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp39-cp39-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.9, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ba21a577ca83a84f74840346b82582b42f5558aea08428bc2f71c7c8aa1c22d |
|
MD5 | 21659698ed830ff20b688026e1b81b40 |
|
BLAKE2b-256 | bae23888cd0b28111e52b3f83073862c9aa8c8244b12ac80d41986ea821338da |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 160.3 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd16ab1c474b192b2aab36556ff7ddf4c57a6e9bea948262699bdb260894187d |
|
MD5 | a34080b145fbe3ca1f0d72bb8b729e06 |
|
BLAKE2b-256 | e8eadc85a2f6041eaebcc9a72cdad27fec8742ff04cceea179ceca178e0ded15 |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 358.8 kB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92bdce204c3fa37da8c2011cb3a6cc729c3bd9a66ab9adb2b2ad90fe1b177e6b |
|
MD5 | a95b454deb177618b53e9635d3d5a443 |
|
BLAKE2b-256 | 99d13b68260100ea96d6bf53e15765b773507bd7ffa349f39c19fbdfb255e2d0 |
File details
Details for the file blosc2_openhtj2k-0.1.2-cp38-cp38-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: blosc2_openhtj2k-0.1.2-cp38-cp38-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.8, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e335a5c20cb7a945f681cf89bb65515280b10809498b1b8ab99abeacf4041fd4 |
|
MD5 | 44d6624ad45217910993e6d0c8ee19f5 |
|
BLAKE2b-256 | bab34fdced4b90e414b9ffd9854ee02f282ef55225a215f89acf80b684c9ba43 |