Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean Way to Fill Third Dimension of Numpy Array

Tags:

python

numpy

I have a 3D numpy array representing an RGB image. I would like to fill the whole image with a particular RGB value. numpy.fill only takes a scalar as an argument-- is there a cleaner way than looping to assign the same third-dimension RGB triplet to each point in the 2d grid?

like image 906
Sean Mackesey Avatar asked Dec 26 '22 10:12

Sean Mackesey


1 Answers

Maybe:

>>> m = np.zeros((2,2,3))
>>> m[:] = [10,20,3]
>>> m
array([[[ 10.,  20.,   3.],
        [ 10.,  20.,   3.]],

       [[ 10.,  20.,   3.],
        [ 10.,  20.,   3.]]])
>>> m[0,0]
array([ 10.,  20.,   3.])
>>> m[0,1]
array([ 10.,  20.,   3.])
>>> m[1,0]
array([ 10.,  20.,   3.])
>>> m[1,1]
array([ 10.,  20.,   3.])
like image 161
DSM Avatar answered Dec 28 '22 15:12

DSM