blosc2.compress(src, typesize=None, clevel=9, filter=Filter.SHUFFLE, codec=Codec.BLOSCLZ)#

Compress src, with a given type size.

  • src (bytes-like object (supporting the buffer interface)) – The data to be compressed.

  • typesize (int (optional) from 1 to 255) – The data type size. The default is 1, or src.itemsize if it exists.

  • clevel (int (optional)) – The compression level from 0 (no compression) to 9 (maximum compression). The default is 9.

  • filter (Filter (optional)) – The filter to be activated. The default is Filter.SHUFFLE.

  • codec (Codec (optional)) – The compressor used internally in Blosc. The default is Codec.BLOSCLZ.


out – The compressed data in form of a Python str / bytes object.

Return type:

str / bytes

  • TypeError – If src doesn’t support the buffer interface.

  • ValueError – If src is too long. If typesize is not within the allowed range. If clevel is not within the allowed range. If codec is not within the supported compressors.


The cname and shuffle parameters in python-blosc API have been replaced by codec and filter respectively. To set codec and filter, the enumerates Codec and Filter have to be used instead of the python-blosc API variables such as blosc.SHUFFLE for filter or strings like “blosclz” for codec.


>>> import array, sys
>>> a = array.array('i', range(1000*1000))
>>> a_bytesobj = a.tobytes()
>>> c_bytesobj = blosc2.compress(a_bytesobj, typesize=4)
>>> len(c_bytesobj) < len(a_bytesobj)