mp.Image

A container for storing an image or a video frame, in one of several formats.

Formats supported by Image are listed in the ImageFormat enum. Pixels are encoded row-major in an interleaved fashion. Image supports uint8, uint16, and float as its data types.

Image can be created by copying the data from a numpy ndarray that stores the pixel data continuously. An Image may realign the input data on its default alignment boundary during creation. The data in an Image will become immutable after creation.

Creation examples:

import cv2
cv_mat = cv2.imread(input_file)
rgb_frame = mp.Image(image_format=mp.ImageFormat.SRGB, data=cv_mat)
gray_frame = mp.Image(
    image_format=mp.ImageFormat.GRAY8,
    data=cv2.cvtColor(cv_mat, cv2.COLOR_RGB2GRAY))

from PIL import Image
pil_img = Image.new('RGB', (60, 30), color = 'red')
image = mp.Image(
    image_format=mp.ImageFormat.SRGB, data=np.asarray(pil_img))

The pixel data in an Image can be retrieved as a numpy ndarray by calling Image.numpy_view(). The returned numpy ndarray is a reference to the internal data and itself is unwritable. If the callers want to modify the numpy ndarray, it's required to obtain a copy of it.

Pixel data retrieval examples:

for channel in range(num_channel):
  for col in range(width):
    for row in range(height):
      print(image[row, col, channel])

output_ndarray = image.</