Set the entire contents of Py5Graphics.np_pixels to the contents of another properly sized and typed numpy array.
import numpy as np def setup(): py5.background(255, 0, 0) array = np.full((50, 50, 1), 240, dtype=np.uint8) g = py5.create_graphics(50, 50) g.begin_draw() g.set_np_pixels(array, bands='L') g.end_draw() py5.image(g, 20, 20)
Set the entire contents of Py5Graphics.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 Py5Graphics drawing surface, respectively. The array’s
dtype must be
np.uint8. This must be used after Py5Graphics.begin_draw() but can be used after Py5Graphics.end_draw().
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),
'RGBA'. If there is no alpha channel,
array is assumed to have no transparency. Unlike the main drawing window, a Py5Graphics drawing surface’s pixels can be transparent so using the alpha channel will work properly. If the
bands parameter is
array’s third dimension is optional.
This method exists because setting the array contents with the code
g.np_pixels = array will cause an error, while the correct syntax,
g.np_pixels[:] = array, might also be unintuitive for beginners.
This method is the same as set_np_pixels() but linked to a
set_np_pixels( 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 ) -> None
Updated on September 01, 2022 16:36:02pm UTC