To do this you can use numpy’s packbits and unpackbits:
import numpy as np
# original boolean array
A1 = np.array([
[0, 1, 1, 0, 1],
[0, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
], dtype=bool)
# packed data
A2 = np.packbits(A1, axis=None)
# checking the size
print(len(A1.tostring())) # 15 bytes
print(len(A2.tostring())) # 2 bytes (ceil(15/8))
# reconstructing from packed data. You need to resize and reshape
A3 = np.unpackbits(A2, count=A1.size).reshape(A1.shape).view(bool)
# and the arrays are equal
print(np.array_equal(A1, A3)) # True
Prior to numpy 1.17.0, the first function is straight-forward to use, but reconstruction required additional manipulations. Here is an example:
import numpy as np
# original boolean array
A1 = np.array([
[0, 1, 1, 0, 1],
[0, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
], dtype=np.bool)
# packed data
A2 = np.packbits(A1, axis=None)
# checking the size
print(len(A1.tostring())) # 15 bytes
print(len(A2.tostring())) # 2 bytes (ceil(15/8))
# reconstructing from packed data. You need to resize and reshape
A3 = np.unpackbits(A2, axis=None)[:A1.size].reshape(A1.shape).astype(np.bool)
# and the arrays are equal
print(np.array_equal(A1, A3)) # True