Reputation: 1651
I want to find the max value of a 3d array in python. I tried
image_file1 = open("lena256x256.bmp","rb")
img_i = PIL.Image.open(image_file1)
pix = numpy.array(img_i);
maxval= max(pix)
but i am getting an error
File "test.py", line 31, in <module>
maxval= max(pix)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I cannot catch my mistake here, please help me.
Upvotes: 7
Views: 30121
Reputation: 25197
You are using the builtin max
function that does not understand multidimensional NumPy arrays. You must instead use one of:
pix.max()
numpy.max(pix)
numpy.amax(pix)
These are also faster than the builtin in the case of 1D NumPy arrays.
Upvotes: 11
Reputation: 685
In accordance to what georgesl wrote, you can use
flat
to get an iterator for the array and then do something
like
m = reduce(max, ar.flat)
Edit: removed the lambda
, the default max
should be OK.
Upvotes: 0
Reputation: 3343
Max is expecting a single value, the error message should be quite clear, you want to use amax
instead.
maxval = numpy.amax(pix)
Upvotes: 4
Reputation: 11002
The np.max function works for vectors, not matrices (or along an axis). To have the max element a multi-dimensionnal array, you can use flatten()
: maxval= pp.max( pix.flatten() )
Upvotes: -3