[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] BGRA to RGB/RGBA, fastest way to do so
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] BGRA to RGB/RGBA, fastest way to do so
- From: Silver <rockachu2@xxxxxxxxx>
- Date: Sat, 01 Sep 2012 13:07:59 -0700
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sat, 01 Sep 2012 16:08:05 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=tWfjmf/HxwjoGTEbi7E7MXg2xI4G5teFPOH4bRhZ0k8=; b=TDvPhmcbolCo2ULzdIDUq8KG6WAyL1dfxy1dvBIi5ubIH4bJ1ymfR/2TAUbgF4QNx8 NDLOlkv9g+U4Iq6fSKe8S73oPZ2pT4AOMujKdpdWc7p+3wg+g2nJoJq/MQ7orSuJGNzB QIhFzTS5z0VjQb7BX+V4/zVgmHdMBsyYr6o9GtfTpym7RzdD0e7sg8bEMb1ZJZBk12A1 ZEOnApJiwCb+EK5sGSuxsQ1YOvYJOujbKlGpqijs93Pd2EyWF0QzuHsalDLAYlhDmKny 4+ZqRu9r7lsSEvGRCZFxuvxjRGuSVhC63YdwFEiGo1VfIhl57BfCqAMrMzu1JTtXuIDv V8xA==
- In-reply-to: <1346529121083-163.post@xxxxxxxxxxxxx>
- References: <1346529121083-163.post@xxxxxxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
- User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
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")