Skip to main content

Image

Inherits from: object A class to represent an image Attributes: data (Union[np.ndarray, bytes]): image data (H x W x C) or raw bytes capture_params (dict, optional): the parameters of image capture img_type (str): type of the image (‘rgb’, ‘depth’, ‘segmentation’) shape (tuple, optional): Shape for bytes data (height, width, channels) dtype (np.dtype, optional): Data type for bytes data

init

Image.__init__(data: np.ndarray, capture_params: Optional[dict] = None, img_type: Optional[str] = None) -> None
Initialize an Image from a numpy array. This is the most common way to create an Image object when you already have image data as a numpy array. The array should be in HWC format (Height, Width, Channels).
Arguments
data (np.ndarray)
np.ndarray
required
Image data as a numpy array with shape (H, W, C) where: - H: Height in pixels - W: Width in pixels - C: Number of channels (e.g., 3 for RGB, 1 for grayscale) The array should have dtype uint8 for standard images, but other dtypes are supported (float32, uint16, etc.)
capture_params (Optional[dict])
Optional[dict]
Dictionary containing metadata about how the image was captured. Can include camera parameters, timestamps, exposure settings, etc. Defaults to None.
img_type (Optional[str])
Optional[str]
Type/format of the image. Common values include: - ‘rgb’: Standard RGB color image - ‘depth’: Depth map image - ‘segmentation’: Semantic segmentation mask - ‘grayscale’: Single channel grayscale image Defaults to None.
Returns
returns
None

init

Image.__init__(data: bytes, shape: tuple, dtype: np.dtype, capture_params: Optional[dict] = None, img_type: Optional[str] = None) -> None
Initialize an Image from raw bytes data. This method is optimized for performance when receiving image data as raw bytes from cameras, network streams, or file I/O. The bytes are efficiently converted to a numpy array using the provided shape and dtype information.
Arguments
data (bytes)
bytes
required
Raw image data in bytes format. This should be the flattened pixel data without any headers or metadata. The total number of bytes should match: height × width × channels × bytes_per_pixel
shape (tuple)
tuple
required
Shape of the image as (height, width, channels). For example: - (480, 640, 3): 480x640 RGB image - (720, 1280, 1): 720x1280 grayscale image - (1080, 1920, 4): 1080x1920 RGBA image
dtype (np.dtype)
np.dtype
required
Data type of the pixel values. Common types include: - np.uint8: Standard 8-bit images (0-255 range) - np.uint16: 16-bit images (0-65535 range, common for depth) - np.float32: Floating point images (often normalized 0.0-1.0) - np.int16: Signed 16-bit (sometimes used for depth with negative values)
capture_params (Optional[dict])
Optional[dict]
Dictionary containing metadata about how the image was captured. Defaults to None.
img_type (Optional[str])
Optional[str]
Type/format of the image (‘rgb’, ‘depth’, ‘segmentation’, etc.). Defaults to None.
Returns
returns
None
Raises
ValueError
If the provided bytes data is too small for the specified shape and dtype combination.
Note
This method is optimized for speed and memory efficiency. It uses np.frombuffer() for fast conversion and only processes the last expected_size bytes if the data is larger than needed. This allows handling of data with headers or padding.

init

Image.__init__(data: Union[np.ndarray, bytes], shape: Optional[tuple] = None, dtype: Optional[np.dtype] = None, capture_params: Optional[dict] = None, img_type: Optional[str] = None)
No docstring provided.
Arguments
data (Union[np.ndarray, bytes])
Union[np.ndarray, bytes]
required
No description provided.
shape (Optional[tuple])
Optional[tuple]
No description provided.
dtype (Optional[np.dtype])
Optional[np.dtype]
No description provided.
capture_params (Optional[dict])
Optional[dict]
No description provided.
img_type (Optional[str])
Optional[str]
No description provided.

to_dict

Image.to_dict() -> dict
Serialize to dictionary for JSON transfer
Returns
returns
dict

from_dict

Image.from_dict(data: dict) -> 'Image'
Deserialize from dictionary - handles both old and new encoded formats
Arguments
data (dict)
dict
required
No description provided.
Returns
returns
'Image'