A container for storing an image or a video frame, in one of several formats.
mp
.
Image
()
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
.
numpy_view
()
print
(
output_ndarray
[
0
,
0
,
0
])
copied_ndarray
=
np
.
copy
(
output_ndarray
)
copied_ndarray
[
0
,
0
,
0
]
=
0
Methods
create_from_file
create_from_file
()
Creates Image
object from the image file.
file_name
Image
object.
is_aligned
is_aligned
()
Return True if each row of the data is aligned to alignment boundary, which must be 1 or a power of 2.
alignment_boundary
image
.
is_aligned
(
16
)
is_contiguous
is_contiguous
()
Return True if the pixel data is stored contiguously (without any alignment padding areas).
is_empty
is_empty
()
Return True if the pixel data is unallocated.
numpy_view
numpy_view
()
Return 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.
output_ndarray
=
image
.
numpy_view
()
copied_ndarray
=
np
.
copy
(
output_ndarray
)
copied_ndarray
[
0
,
0
,
0
]
=
0
uses_gpu
uses_gpu
()
Return True if data is currently on the GPU.
__getitem__
__getitem__
()
Use the indexer operators to access pixel data.
IndexError
for
channel
in
range
(
num_channel
):
for
col
in
range
(
width
):
for
row
in
range
(
height
):
print
(
image
[
row
,
col
,
channel
])

