Python Forum

Full Version: Still struggling with np.where...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone

Despite some help on this forum I am still struggling with np.where

Can someone help solve my problem?

I have a numpy array derived from an image which is 1024 x 1024 x 4 in size. eg [10,11,0] is the red attribute of the pixel located at x axis 11 y axis 10 (yes its reversed).

Lets assume that my array has various values all less than 253 in all the array except for the locations 10,11 and 23,24 and 83,89. I want to use np.where to find the locations of pixels where [x,y,0]>252.

How can I do this using np.where ?
The output I want to generate is a list of positions i.e [[10,11], [23,24],[83,89]} so it may be necessary to convert tuple into a list and then swap the x and y axis.

I have tried using
new_list = img[np.where(img[:, :][0] >252)]
I get the following error
Output:
TypeError: 'builtin_function_or_method' object is not subscriptable
but this is not working for me.
Thanks Peter
np.where returns lists of indices where the condition is met. So, if you correctly apply np.where to your data, you get something like this: (array([10, 23, 83]), array([11, 24, 89])). The latter could be converted to [[10, 11], [23, 24]...] using zip:

list(zip(*np.where(img[..., 0]>252)))