Vamsi Krishna Vamsi Krishna - 11 months ago 54
Python Question

Convert a RGBA numpy array into binary 2d numpy array

I have loaded image as a numpy array using pillow and the dimensions of the resulting numpy array is (184, 184, 4), i.e it's a 184x184 pixel image with each pixel having RGBA dimensions.

For all practical purposes my application just needs to know if a pixel is black or not and hence I just need a 184x184 np array with 0, 1's.

I am new to numpy and particularly image manipulation, wanted to know if there is a faster method to do it.

I tried the naive implementation of checking each pixel in a loop, which appears to be too costly.

Answer Source

If I understand correctly, you have an array with shape (184,184,4) and you want to get a boolean array with shape (184,184) depending on whether the final dimension is equal to [0,0,0,0]

>>> image = np.random.randint(0,255,size=(184,184,4)) #Generate random image
>>> isBlack = np.int64(np.all(image[:,:,:3]==0,axis=2))


But how does it work? It seems like magic! Numpy is kind of magical. That's why we like it. In this case:

  • the image==0 converts to a (184,184,4) boolean array depending on whether each number is equal to 0
  • in this case we use image[:,:,:3] to exclude the 4th number from the equation
  • the np.all asks whether all the elements are equal to 0.
  • the axis parameter changes the axis that it looks at, remembering that python starts counting at 0, so the third axis is number 2.