Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Image Pixel Access in Python

In Python, CVB provides a convenient way to access pixel data by either mapping an image to a NumPy array (if possible) or copying it. The pixel values of the NumPy array can be accessed through np_array[row, col, plane].

There are two ways to obtain the NumPy array and modify its pixel values, depending on the underlying memory layout:

NumPy ArrayCVB ImageNumPy ArrayCVB Imagecvb.as_arraycvb.WrappedImage.from_buffer

Attention
As a user, it is important to manage the lifetime of objects carefully. It is recommended to use Python's with statement for all objects that hold resources with critical lifetimes, such as devices and images, to ensure proper resource management.

Example Program

Additionally to the code in this section, a sample program can be found in the CVB installation under %cvb%Tutorial\Image Manager\CVBpy\ImagePixelAccess. Alternatively, the example can be accessed via the following link: ImagePixelAccess

Mapping Image to NumPy Array Without Copying

Mapping an image to a NumPy array allows direct access to pixel data without creating a copy, making it an efficient approach whenever supported.

The following example demonstrates how to map an RGB image to a NumPy array:

import cvb
width = 16
height = 4
# Create an RGB image.
with cvb.Image.from_pixel_format(width, height, cvb.PfncFormat.RGB8) as image: # (1)
# Map the image to a NumPy array without copying (copy=False by default).
np_array = cvb.as_array(image, copy=False) # (2)
# Verify if the mapping was successful.
if np_array.flags["OWNDATA"]: # (3)
raise RuntimeError("cannot map to numpy array")
# Modify pixel values (blue image).
np_array[:,:,0] = 0 # (4)
np_array[:,:,1] = 0
np_array[:,:,2] = 255
# Save image.
image.save("blue_image.tif") # (5)
cvb.Image from_pixel_format(cvb.Size2D size, int format)
numpy.array as_array(Any buffer, bool copy=False)

(1) An RGB 8-bit unsigned image is created. The with statement should be used to manage the image's lifetime automatically.
(2) The image is mapped to a NumPy array without copying, allowing direct access to pixel data.
(3) Check whether the mapping was successful; if OWNDATA is True, the image could not be mapped. In this case, the image has to be copied by setting copy=True in previous call (see the next example).
(4) Modify all pixel values in the NumPy array, which directly updates the underlying image data.
(5) Save the modified image to a file.

Copying Image to NumPy Array and Mapping it to a Wrapped Image

Whenever mapping the image is not possible, it must be copied instead. Note, when copying an image to a NumPy array, the pixel data is duplicated, meaning modifications to the NumPy array do not affect the original image data. To continue processing the modified image, the NumPy array can be mapped to a cvb.WrappedImage.

image = ...
# Create a copy of the image data as a NumPy array.
np_array = cvb.as_array(image, copy=True) # (1)
# Modify pixel values (green image).
np_array[:,:,0] = 0 # (2)
np_array[:,:,1] = 255
np_array[:,:,2] = 0
# Since copy=True, modifications to the NumPy array do not affect the original image buffer.
# Therefore map the array to a WrappedImage:
with cvb.WrappedImage.from_buffer(np_array, cvb.BufferLayout.HeightWidthPlane) as image_out: # (3)
image_out.save("green_image.tif") # (4)
cvb.WrappedImage from_buffer(Any buffer, int layout=cvb.BufferLayout.HeightWidthPlane)

(1) Copy the image data to a new NumPy array, being aware that modifications do not affect the original image.
(2) Modify the copied NumPy array; since the image was copied, the original data remains unchanged.
(3) Map the NumPy array to a cvb.WrappedImage without copying. Use cvb.BufferLayout.HeightWidthPlane as the layout for NumPy arrays. The with statement should be used for lifetime management.
(4) Save the modified image to a file.