[Numpy-discussion] numpy.choose question

Christopher Barker Chris.Barker@noaa....
Sat May 23 14:51:24 CDT 2009

Mark Wendell wrote:
> I'm working with rgb image arrays (converted using PIL's 'asarray'),
> and would like to combine data from multiple images. The choose method
> seemed just the thing for what i want to do: creating new images from
> multiple source images and an index mask. However, it looks like the
> choose method chokes on anything but single-value data arrays. As soon
> as I give it source arrays made up of RGB int triplets (e.g.,
> [0,128,255]), it complains with a "ValueError: too many dimensions"
> error.

you might try making a 2d array of a a datatype that fits the rgb triples:

 >>> rgb = np.dtype([('r', np.uint8),('g', np.uint8),('b',np.uint8)])
 >>> rgb
dtype([('r', '|u1'), ('g', '|u1'), ('b', '|u1')])

 >>> image = np.ones((5,6,3),dtype=np.uint8)
(that's a 5x3 rgb image, as PIL should have created it...)

 >>> image2 = image.view(dtype=rgb).reshape((5,6))
 >>> image2.shape
(5, 6)

now it's a 2-d array, and you may be able to use choose, like you expected.


Christopher Barker, Ph.D.

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

More information about the Numpy-discussion mailing list