[Numpy-discussion] can i improve this code?

Pete Shinners pete at visionart.com
Tue Sep 19 18:55:59 EDT 2000

I have image data stored in a 3D array. the code is pretty
simple, but what i don't like about it is needing to transpose
the array twice. here's the code...

def map_rgb_array(surf, a):
    "returns an array of 2d mapped pixels from a 3d array"
    #image info needed to convert map rgb data
    loss = surf.get_losses[:3]
    shift = surf.get_shifts()[:3]
    prep = a >> loss << shift
    return transpose(bitwise_or.reduce(transpose(prep)))

once i get to the 'prep' line, my 3d image has all the
values for R,G,B mapped to the appropriate bitspace.
all i need to do is bitwise_or the values together
"finalcolor = R|G|B". this is where my lack of guru-ness
with numpy comes in. the only way i could figure to do
this was transpose the array and then apply the bitwise_or.
only problem is then i need to de-transpose the array to
get it back to where i started. is there some form or mix
of bitwise_or i can use to make it operate on the 3rd
axis instead of the 1st?

thanks all, hopefully this can be improved. legibility
comes second to speed as a priority :]

... hmm, tinkering time passes ...

i did find this, "prep[:,:,0] | prep[:,:,1] | prep[:,:,2]".
is something like that going to be best?
"bitwise_or.reduce((prep[:,:,0], prep[:,:,1], prep[:,:,2]))"

More information about the NumPy-Discussion mailing list