Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a png image to a numpy array, what is the 4th column last dimension?

I exported a png image to a python numpy array.

import numpy
import Image
import matplotlib.pyplot as plt
 Im=Image.opne('file.png')
 arr=numpy.array(Im).reshape(Im.size[1],Im.size[0],4)
plt.imshow(arr)
plt.show()

Now, in books I found reshape of png using 3 channels, which does not work for me. I had to use 4.

>>> Im.shape
(401, 601, 4)

I can visualize the Red channel :

ImR=Im[:,:,0]

Similar for Green and Blue. The last one give me a white screen.

I managed to remove it:

Im4=Im[:,:,:-1]

and now:

>>> Im4.shape
(401, 601, 3)

I can visualize the image as before removing this 4th column

My question is: what is this 4th column for in PNG?

like image 518
daniel_hck Avatar asked Oct 16 '25 17:10

daniel_hck


1 Answers

If the PNG had an alpha channel, the fourth column would be alpha. Since visualizing that channel gave you all-white, all of the alphas are maxval (255 if your samples are 8-bit) which means all pixels are opaque.

like image 83
Glenn Randers-Pehrson Avatar answered Oct 19 '25 05:10

Glenn Randers-Pehrson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!