By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Optimizing numpy

P: n/a
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.

May 12 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On May 12, 10:52 pm, Gerdus van Zyl <gerdusvan...@gmail.comwrote:
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
May 12 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.