Optimizing numpy

G

Gerdus van Zyl

I have the following, that is used to convert pixel data and thus
should be as fast as possible:

b = numpy.ndarray (shape=(w,h,4), dtype=numpy.uint8)

a = numpy.frombuffer(buf, numpy.uint8)
a.shape = (w, h, 3)

b[:,:,0] = a[:,:,2]
b[:,:,1] = a[:,:,1]
b[:,:,2] = a[:,:,0]
b[:,:,3] = 255

Can anyone tell me if there is a faster way? Will making use of
weave.blitz or pyrex help?

Thank You.
 
S

sturlamolden

I have the following, that is used to convert pixel data and thus
should be as fast as possible:

b = numpy.ndarray (shape=(w,h,4), dtype=numpy.uint8)

a = numpy.frombuffer(buf, numpy.uint8)
a.shape = (w, h, 3)

b[:,:,0] = a[:,:,2]
b[:,:,1] = a[:,:,1]
b[:,:,2] = a[:,:,0]
b[:,:,3] = 255

You can express this as:

b[:,:,0:3] = a[:,:,2:-1:-1]
b[:,:,3] = 255

Can anyone tell me if there is a faster way? Will making use of
weave.blitz or pyrex help?


If you are going to use wave, then don't bother with weave.blitz use
wave.inline instead. You'll need something like this:

code = """
register char a0, a1, a2;
for (int i=0; i<w*h; i++) {
a0 = *a++;
a1 = *a++;
a2 = *a++;
*b++ = a2;
*b++ = a1;
*b++ = a0;
*b++ = 0xFF;
}
"""
wave.inline(code,['a','b','h','w'],compiler='gcc')


Sturla Molden
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top