Py5Image.set_np_pixels()#

Set the entire contents of Py5Image.np_pixels[] to the contents of another properly sized and typed numpy array.

Examples#

 1import numpy as np
 2
 3def make_array(value):
 4    return np.full((50, 50, 1), value, dtype=np.uint8)
 5
 6def setup():
 7    global img
 8    py5.image_mode(py5.CENTER)
 9    img = py5.create_image(50, 50, py5.RGB)
10
11def draw():
12    py5.background(128)
13    array = make_array(py5.frame_count % 256)
14    img.set_np_pixels(array, bands='L')
15    py5.image(img, py5.mouse_x, py5.mouse_y)

Description#

Set the entire contents of Py5Image.np_pixels[] to the contents of another properly sized and typed numpy array. The size of array’s first and second dimensions must match the height and width of the image, respectively. The array’s dtype must be np.uint8.

The bands parameter is used to interpret the array’s color channel dimension (the array’s third dimension). It can be one of 'L' (single-channel grayscale), 'ARGB', 'RGB', or 'RGBA'. If there is no alpha channel, array is assumed to have no transparency. If the bands parameter is 'L', array’s third dimension is optional.

This method makes its own calls to Py5Image.load_np_pixels() and Py5Image.update_np_pixels() so there is no need to call either explicitly.

This method exists because setting the array contents with the code img.np_pixels = array will cause an error, while the correct syntax, img.np_pixels[:] = array, might also be unintuitive for beginners.

Note that the convert_image() method can also be used to convert a numpy array into a new Py5Image object.

Syntax#

set_np_pixels(array: npt.NDArray[np.uint8], bands: str = 'ARGB') -> None

Parameters#

  • array: npt.NDArray[np.uint8] - properly sized numpy array to be copied to np_pixels[]

  • bands: str = ‘ARGB’ - color channels in the array’s third dimension

Updated on March 22, 2022 21:53:01pm UTC