[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] BGRA to RGB/RGBA, fastest way to do so



On 9/1/2012 12:52 PM, gm770 wrote:
I'm trying to convert a color string buffer, and looking for the fastest way
to do so.
I'm capturing a 2 screen desktop (h=1080, w=3840), but it comes in as BGRA,
and I don't see an obvious way to convert that to RGBA or RGB.

The fastest way I have so far is ~1.78 sec, but I'm sure there must be
something faster.

def toRGBA(BGRA_str):
         buf = bytearray(BGRA_str)
         for x in xrange(0,len(buf),4):
                 buf[x], buf[x+2] = buf[x+2], buf[x]
         return buf

Slower methods include:
- starting with an empty array, and via loop, appending to create RGB. (~3.8
sec)
- turn the buffer to a list of chars, swap and return the joined results.
(~2.14 sec)




Can you use numpy?

RGBA_str = numpy.fromstring(BGRA_str, dtype='uint8').reshape((3840*1080, 4))[:, (2, 1, 0, 3)].tostring()

Christoph