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.
The max() Function — Find the Largest Element of a List. In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.
In Python to initialize a 3-dimension array, we can easily use the np. array function for creating an array and once you will print the 'arr1' then the output will display a 3-dimensional array.
maximum() function is used to find the element-wise maximum of array elements. It compares two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then that element is returned.
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.
Max is expecting a single value, the error message should be quite clear, you want to use amax
instead.
maxval = numpy.amax(pix)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With