View source on GitHub
|
A container for storing an image or a video frame.
mp.Image(
image_format: 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(). 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
Args | |
|---|---|
image_format
|
The format of the image data. |
data
|
A numpy ndarray containing the image data. |
Methods
create_from_file
@classmethodcreate_from_file( file_name: str ) -> 'Image'
Creates an Image object from an image file.
| Args | |
|---|---|
file_name
|
The path to the image file. |
| Returns | |
|---|---|
An Image object.
|
| Raises | |
|---|---|
RuntimeError
|
If the image file cannot be decoded. |
is_aligned
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.
| Args | |
|---|---|
alignment_boundary
|
An integer. |
| Returns | |
|---|---|
| A boolean. |
| Examples | |
|---|---|
|
is_contiguous
is_contiguous() -> bool
Return True if the pixel data is stored contiguously (without any alignment padding areas).
is_empty
is_empty() -> bool
Return True if the pixel data is unallocated.
numpy_view
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.
| Returns | |
|---|---|
| An unwritable numpy ndarray. |
| Examples | |
|---|---|
|
uses_gpu
uses_gpu() -> bool
Return True if data is currently on the GPU..
__getitem__
__getitem__(
key: tuple[int, ...]
) -> Any
Use the indexer operators to access pixel data.
| Args | |
|---|---|
key
|
A tuple of integers representing the row, column, and channel indices (or row and column for single channel images). |
| Returns | |
|---|---|
| The pixel data at the specified index. |
| Raises | |
|---|---|
IndexError
|
If the index is invalid or out of bounds. |
| Examples | |
|---|---|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Last updated 2026-06-05 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-06-05 UTC."],[],[]]
|
View source on GitHub