Hugo
Hugo

Reputation: 1688

How can I get one column of a Numpy array?

I am triyng to get the 3 columns of a NumPy (RGB) array:

print px
[[[  0   0   0]
  [255 255 255]
  [255   0   0]
  [  0 255   0]
  [  0   0 255]]]

print px[:,0]
print px[:,1]
print px[:,2]

[[0 0 0]]
[[255 255 255]]
[[255   0   0]]

but I would like to get the R, G and B like

[[0 255 255 0 0]]
[[0 255 0 255 0]]
[[0 255 0 0 255]]

Could you help me?

Thank you

Hugo

Upvotes: 0

Views: 1472

Answers (1)

Carsten
Carsten

Reputation: 18446

Your array px is three-dimensional: the first dimension has just a single element: the complete arrays containing rows and colums. The second dimension is rows, the third is colums. Therefore, to select a column, and have it embedded in the outermost dimension like you have, use the following:

>>> print px[:,:,0]
[[  0 255 255   0   0]]

>>> print px[:,:,1]
[[  0 255   0 255   0]]

>>> print px[:,:,2]
[[  0 255   0   0 255]]

Upvotes: 3

Related Questions