blosc2.compress2#

blosc2.compress2(src: object, **kwargs: dict) str | bytes#

Compress the given src buffer with the specified compression parameters.

Parameters:
  • src (bytes-like object) – The buffer to compress. Must support the buffer interface.

  • kwargs (dict, optional) –

    Compression parameters. The default values are in blosc2.CParams. Supported keyword arguments:

    cparams: blosc2.CParams or dict

    All the compression parameters to use, provided as a blosc2.CParams instance or dictionary.

    others: Any

    If cparams is not provided, all the parameters of a blosc2.CParams can be passed as keyword arguments.

Returns:

out – The compressed data as a Python str or bytes object.

Return type:

str or bytes

Raises:

RuntimeError – If the data cannot be compressed into dst. If an internal error occurs, likely due to an invalid parameter.

Notes

This function only can deal with data < 2 GB. If you want to compress larger buffers, you should use the SChunk class or, if you want to save large arrays/tensors, the pack_tensor() function can be handier.

Examples

>>> import numpy as np
>>> data = np.arange(1e6, dtype=np.float32)
>>> cparams = blosc2.CParams()
>>> compressed_data = blosc2.compress2(data, cparams=cparams)
>>> print(f"Compressed data length: {len(compressed_data)} bytes")
Compressed data length: 14129 bytes

See also

decompress2(), pack_tensor(), SChunk