1

I solved a problem with a loop, but it is both slow and unpythonic. I'm looking for a mask solution. If I were interested in pixels with values for a specific channel, that would be simple:

img[img[:,:,0]==64] = [0,0,0]

to turn them all black. I want to change a specific color, e.g. [192,0,128], so I need all three channels, something like img[ img[:,:,0]==192 and img[:,:,1]==0 and img[:,:,2]==128]=[0,0,0] but of course this is wrong. I also tried np.all(img==[192,0,128]) but it didn't work either.

2 Answers 2

7

You were almost there:

np.all(img == [192,0,128], axis=-1)

gives what you're looking for. You need to specify an axis to do the dimension reduction, which corresponds to the color channel axis here.

0

OK, I just used numpy.logical_and() and it did the trick!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.