np_pixels array contains the values for all the pixels in the image.
1def setup(): 2 tower = py5.load_image("tower.jpg") 3 tower.load_np_pixels() 4 tower.np_pixels[:, ::2, 1:] = [0, 0, 0] 5 tower.update_np_pixels() 6 py5.image(tower, 0, 0)
np_pixels array contains the values for all the pixels in the image. Unlike the one dimensional array Py5Image.pixels, the
np_pixels array organizes the color data in a 3 dimensional numpy array. The size of the array’s dimensions are defined by the size of the image. The first dimension is the height, the second is the width, and the third represents the color channels. The color channels are ordered alpha, red, green, blue (ARGB). Every value in
np_pixels is an integer between 0 and 255.
This numpy array is very similar to the image arrays used by other popular Python image libraries, but note that some of them like opencv will by default order the color channels as RGBA.
Much like the Py5Image.pixels array, there are load and update methods that must be called before and after making changes to the data in
np_pixels. Before accessing
np_pixels, the data must be loaded with the Py5Image.load_np_pixels() method. If this is not done,
np_pixels will be equal to
None and your code will likely result in Python exceptions. After
np_pixels has been modified, the Py5Image.update_np_pixels() method must be called to update the content of the display window.
To set the entire contents of
np_pixels to the contents of another equally sized numpy array, consider using Py5Image.set_np_pixels().
Updated on September 11, 2021 16:51:34pm UTC