Skip to content
Advertisement

Replace pixel value in RGBA numpy array

I have a 2D array of RGBA values (Ex: [30, 60, 90, 255]) and I want to replace all white [255 255 255 255] with [0 0 0 0]. What is the simplest way to do this?

Using for loops I have tried assigning a new array to an index but the index does not change:

JavaScript

Advertisement

Answer

You can use np.all to check all four values for each row and column:

JavaScript

Get the index mask with a boolean condition against [255, 255, 255, 255]:

JavaScript

At this point, you can either assign a four element list or one element using mask:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement