Ternary operator on arrays in python -


i trying perform following operation on array in python:

if true else b 

i trying perform on 1 channel of image. want check if value greater 255, if so, return 255 else return value being checked.

here i'm trying:

imfinal[:,:,1] = imfinal[:,:,1] if imfinal[:,:,1] <= 255 else 255 

i following error: valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all()

is there better way perform operation?

use np.where:

imfinal[:,:,1] = np.where(imfinal[:,:,1] <= 255, imfinal[:,:,1], 255) 

as why error see this: valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all().

essentially becomes ambiguous when compare arrays using and, or because if 1 value in array matches? compare arrays should use bitwise operators &, |, ~ and, or, not respectively.

np.where uses boolean condition assign value in second param when true, else assigns 3rd param, see docs


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -