<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L200-L565) |

A container for storing an image or a video frame.

    mp.Image(
        image_format: https://developers.google.com/edge/api/mediapipe/python/mp/ImageFormat,
        data: np.ndarray
    )

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. The data in an Image will become immutable
after creation.

The pixel data in an Image can be retrieved as a numpy ndarray by calling
[`Image.numpy_view()`](https://developers.google.com/edge/api/mediapipe/python/mp/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.numpy_view()
    print(output_ndarray[0, 0, 0])
    copied_ndarray = np.copy(output_ndarray)
    copied_ndarray[0,0,0] = 0

<br />

<br />

<br />

| ## Args ||
|---|---|
| `image_format` | The format of the image data. |
| `data` | A numpy ndarray containing the image data. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `channels` | Returns the number of channels in the image. |
| `height` | Returns the height of the image. |
| `image_format` | Returns the image format. |
| `step` | Returns the width step of the image. |
| `width` | Returns the width of the image. |

<br />

## Methods

### `create_from_ctypes`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L330-L363)

    @classmethod
    create_from_ctypes(
        image_ptr: ctypes.c_void_p
    ) -> 'Image'

Creates an `Image` object from a ctypes pointer.

This methods creates a copy of the Image object, which points to the same
underlying image data and extends its lifetime until the returned object
gets garbage collected. If the original image is on GPU, the copy will
allocate the pixel data on the CPU.

<br />

<br />

<br />

| Args ||
|---|---|
| `image_ptr` | A ctypes pointer to the image data. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An `Image` object. ||

<br />

### `create_from_file`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L296-L328)

    @classmethod
    create_from_file(
        file_name: str
    ) -> 'Image'

Creates an `Image` object from an image file.

<br />

<br />

<br />

| Args ||
|---|---|
| `file_name` | The path to the image file. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An `Image` object. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `RuntimeError` | If the image file cannot be decoded. |

<br />

### `free_ctypes`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L365-L374)

    @classmethod
    free_ctypes(
        image_ptr: ctypes.c_void_p
    ) -> None

Frees a ctypes pointer to an image.

<br />

<br />

<br />

| Args ||
|---|---|
| `image_ptr` | A ctypes pointer to the image data. |

<br />

### `is_aligned`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L521-L535)

    is_aligned(
        alignment_boundary: int
    ) -> bool

Return True if each row of the data is aligned to alignment boundary, which must be 1 or a power of 2.

<br />

<br />

<br />

| Args ||
|---|---|
| `alignment_boundary` | An integer. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A boolean. ||

<br />

<br />

<br />

<br />

| Examples ||
|---|---|
| `image.is_aligned(16)` ||

<br />

### `is_contiguous`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L513-L515)

    is_contiguous() -> bool

Return True if the pixel data is stored contiguously (without any alignment padding areas).

### `is_empty`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L517-L519)

    is_empty() -> bool

Return True if the pixel data is unallocated.

### `numpy_view`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L376-L437)

    numpy_view() -> np.ndarray

Returns the image pixel data as an unwritable numpy ndarray.

Realign the pixel data to be stored contiguously and return a reference to
the unwritable numpy ndarray. If the callers want to modify the numpy array
data, it's required to obtain a copy of the ndarray.

<br />

<br />

<br />

| Returns ||
|---|---|
| An unwritable numpy ndarray. ||

<br />

<br />

<br />

<br />

| Examples ||
|---|---|
| `output_ndarray = image.numpy_view() copied_ndarray = np.copy(output_ndarray) copied_ndarray[0,0,0] = 0` ||

<br />

### `uses_gpu`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L509-L511)

    uses_gpu() -> bool

Return True if data is currently on the GPU..

### `__getitem__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/vision/core/image.py#L439-L507)

    __getitem__(
        key: tuple[int, ...]
    ) -> Any

Use the indexer operators to access pixel data.

<br />

<br />

<br />

| Args ||
|---|---|
| `key` | A tuple of integers representing the row, column, and channel indices (or row and column for single channel images). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The pixel data at the specified index. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `IndexError` | If the index is invalid or out of bounds. |

<br />

<br />

<br />

<br />

| Examples ||
|---|---|
| \`\`\` for channel in range(num_channel): for col in range(width): for row in range(height): print(image\[row, col, channel\]) ||

<br />