7 assert data.ndim == 1
or data.ndim == 2
17 numpy_type_to_cvtype = {
'uint8': 0,
'int8': 1,
'uint16': 2,
18 'int16': 3,
'int32': 4,
'float32': 5,
21 compressed_data = bytearray(zlib.compress(data.tobytes()))
22 compressed_data.extend(struct.pack(
"iii", dim1, dim2, numpy_type_to_cvtype[data.dtype.name]))
24 return compressed_data
27 cvtype_to_numpy_type = {0:
'uint8', 1:
'int8', 2:
'uint16',
28 3:
'int16', 4:
'int32', 5:
'float32',
30 out = zlib.decompress(bytes[:len(bytes)-3*4])
31 rows, cols, datatype = struct.unpack_from(
"iii", bytes, offset=len(bytes)-3*4)
32 data = np.frombuffer(out, dtype=cvtype_to_numpy_type[datatype])
33 return data.reshape((rows, cols))