[Numpy-discussion] numpy.choose question
Sat May 23 16:09:25 CDT 2009
On Sat, May 23, 2009 at 10:14, Mark Wendell <firstname.lastname@example.org> wrote:
> I have a question about the numpy.choose method.
> 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"
> My hope was that the choose method would return an array made up of
> the same kind of elements that the source arrays are made up of (in
> this case, RGB triplets), but it seems to only like scalars. I guess
> it sees my image arrays at 3D arrays, and doesn't know what to do with
> them. Is that right?
Yes. They are 3D arrays. Try not to think of them as 2D arrays of RGB
triplets. You can check these things by looking at the .shape
attribute of the array.
> I suppose I could pre-split my images into
> separate R, G, and B arrays, run them through np.choose, and then
> recombine the results, but that seems clunky.
Make your index array the same shape as the image array.
dstack([indices] * 3) should do the trick.
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion