array_to_json#

k3d.helpers.array_to_json(ar, compression_level=0, force_contiguous=True)[source]#

Return the serialization of a numpy array.

Parameters
  • ar (ndarray) – A numpy array.

  • compression_level (int, optional) – Level of compression [-1, 9], by default 0.

  • force_contiguous (bool, optional) – Make the array contiguous in memory, by default True.

Returns

Binary data of the array with its dtype and shape.

Return type

dict

Raises

ValueError – Unsupported dtype.

Examples#

Data compression#

import k3d
import numpy as np

ar = np.array([[1, 2, 3], [4, 5, 6]])

data = k3d.helpers.array_to_json(ar, compression_level=2)

"""
{
  'compressed_data': b'x^cd```\x02bf f\x01bV f\x03b\x00\x00\xf8\x00\x16',
  'dtype': 'int32',
  'shape': (2, 3)
}
"""

No data compression#

import k3d
import numpy as np

ar = np.array([[1, 2, 3], [4, 5, 6]])

data = k3d.helpers.array_to_json(ar)

"""
{
  'data': <memory at 0x7faaae2a1e80>,
  'dtype': 'int32',
  'shape': (2, 3)
}
"""