8

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.

0

4 Answers 4

11

You are using the builtin max function that does not understand multidimensional NumPy arrays. You must instead use one of:

These are also faster than the builtin in the case of 1D NumPy arrays.

Sign up to request clarification or add additional context in comments.

Comments

4

Max is expecting a single value, the error message should be quite clear, you want to use amax instead.

maxval = numpy.amax(pix)

Comments

0

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.

Comments

-3

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() )

1 Comment

This is not correct. numpy.max is an alias to numpy.amax that understands multidimensional arrays. The OP, however, is not using numpy.max.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.