pydicom.pixels.utils.pack_bits

pydicom.pixels.utils.pack_bits(arr: ndarray, pad: bool = True) bytes[source]

Pack a binary numpy.ndarray for use with Pixel Data.

Should be used in conjunction with (0028,0100) Bits Allocated = 1.

Changed in version 2.1: Added the pad keyword parameter and changed to allow arr to be 2 or 3D.

Parameters:
  • arr (numpy.ndarray) – The numpy.ndarray containing 1-bit data as ints. arr must only contain integer values of 0 and 1 and must have an ‘uint’ or ‘int’ numpy.dtype. For the sake of efficiency it’s recommended that the length of arr be a multiple of 8 (i.e. that any empty bit-padding to round out the byte has already been added). The input arr should either be shaped as (rows, columns) or (frames, rows, columns) or the equivalent 1D array used to ensure that the packed data is in the correct order.

  • pad (bool, optional) – If True (default) then add a null byte to the end of the packed data to ensure even length, otherwise no padding will be added.

Returns:

The bit packed data.

Return type:

bytes

Raises:

ValueError – If arr contains anything other than 0 or 1.

References

DICOM Standard, Part 5, Section 8.1.1 and Annex D