Re: Convert RGBA to BGRA using standard library?

geoff <[email protected]>
Newsgroups gmane.comp.python.pygui,gmane.comp.gnome.gtk+.python,gmane.comp.python.pyobjc.devel,gmane.comp.python.windows
Message-ID <[email protected]>
On Sat, May 28, 2011 at 5:55 AM, Greg Ewing <greg.ewing-F+z8Qja7x9Xokq/[email protected]>wrote:

> Can anyone think of an efficient way to convert a string
> full of RGBA image data to BGRA, using only what's available
> in the standard library?
>
> I'm trying to add a function to PyGUI for creating an Image
> object from arbitrary data. The problem I'm having is that
> GDI+ on Windows expects BGRA, whereas most other platforms
> deal with RGBA. I don't want to require the user to supply
> the data in different formats on different platforms, so
> PyGUI needs to be able to convert where necessary.
>
> I know the conversion can be done easily using something
> like PIL or numpy, but I'm after a solution that doesn't
> depend on any third-party libraries.
>
> --
> Greg
> ______________________________**_________________
> Pygui mailing list
> [email protected]
> http://mail.python.org/**mailman/listinfo/pygui<http://mail.python.org/mailman/listinfo/pygui>
>


Greg, I had to solve this problem in another application and ended up using
the array module and the with the slice syntax.

import array

input = "rgbaRGBA1234"
ba = array.array('c', input)
ba[0::4], ba[2::4] = ba[2::4], ba[0::4]

print ba.tostring()

>> bgraBGRA3214

It is fairly fast too.

Good luck and keep up the great work.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.