[Numpy-discussion] Broadcasting question

Chris.Barker Chris.Barker@noaa....
Thu May 1 23:58:34 CDT 2008


Hi all,

I have a n X m X 3 array, and I a n X M array. I want to assign the 
values in the n X m to all three of the slices in the bigger array:

A1 = np.zeros((5,4,3))
A2 = np.ones((5,4))
A1[:,:,0] = A2
A1[:,:,1] = A2
A1[:,:,2] = A2

However,it seems I should be able to broadcast that, so I don't have to 
repeat myself, but I can't figure out how.

The full question:

I have a (w,h) array of ints, representing a grey scale image. I'm 
trying to convert that into a RBG image. I've played with defining the 
image array as a rec array or a h,w,3 uint8 array. neither one lets me 
assign the three bands to the same value in one step:

import numpy as np

w = 2
h = 2

grey = np.arange(w*h).reshape((w,h))

RGBrec = np.dtype({'r':(np.uint8,0),
                    'g':(np.uint8,1),
                    'b':(np.uint8,2),
                    })

ImageRGB = np.zeros((w,h), dtype=RGBrec)
ImageInt = Image1.view((np.uint8, 3))

## neither of these work:
#ImageRGB[:,:] = grey
#ImageInt[:,:] = grey
print ImageRGB

How else might I do this?

-Chris




-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the Numpy-discussion mailing list