numpy.packbits

numpy.packbits(myarray, axis=None)

myarray : an integer type array whose elements should be packed to bits

This routine packs the elements of a binary-valued dataset into a NumPy array of type uint8 (‘B’) whose bits correspond to the logical (0 or nonzero) value of the input elements. The dimension over-which bit-packing is done is given by axis. The shape of the output has the same number of dimensions as the input (unless axis is None, in which case the output is 1-d).

Example: >>> a = array([[[1,0,1], ... [0,1,0]], ... [[1,1,0], ... [0,0,1]]]) >>> b = numpy.packbits(a,axis=-1) >>> b array([[[160],[64]],[[192],[32]]], dtype=uint8)

Note that 160 = 128 + 32
192 = 128 + 64

Previous topic

numpy.right_shift

Next topic

numpy.unpackbits

This Page

Quick search