I'm trying to get a 2d numpy array with grayscale values from RGBA image. The method I use is imread
from spicy.misc
, but whenever I set mode='F'
or flatten=True
the result is a zero matrix.
My code:
img_mat = misc.imread(f, mode='F')
also tried
img_mat = misc.imread(f, flatten=True)
Output:
(278, 278)
[[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
...,
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]]
And the image: letter A
Your image is "pure alpha"! The RGB components of the image are all 0; only the alpha channel has nonzero values. Apparently, when the underlying image library (PIL or Pillow) converts an RGBA image to mode 'F', it ignores the alpha channel, so you end up with an array of zeros.
Instead of flattening the image when you read it, you could read the RGBA data, and then use the alpha channel as your flattened image:
img_data = imread(f) # Read the RGBA image
img_mat = img_data[:, :, 3] # Extract the alpha channel