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) > > > > -- > View this message in context: > http://pygame-users.25799.n6.nabble.com/BGRA-to-RGB-RGBA-fastest-way-to-do-so-tp163.html > Sent from the pygame-users mailing list archive at Nabble.com. > >From the doc page comments: As for "BGR" (OpenCV): Just use "RBG" but reverse the string first and then flip the surface (vertically and horizontally).
I am using this with fromstring: frame = cvQueryFrame(capture) # get a video frame using OpenCV bgr = frame.imageData # this is a string using BGR rgb = bgr[::-1] # reverse it to get RGB im = pygame.image.fromstring(rgb, size, 'RGB') # create pygame surface im = pygame.transform.flip(im, True, True) # flip it you could also use map(): x = [buf[x], buf[x+2] for x in xrange(0, len(buf), 4)]## convert to list format map(lambda x: [x[2], x[1], x[0], x[3]], bgra) ## returns a list of four-item lists in RGBA format. or you could import/export it from pygame: ARGB = BGRA[::-1] ARGB_image = pygame.image.fromstring(ARGB, size, "ARGB") RGBA_str = pygame.image.tostring(ARGB_image, "RGBA")
