[Image-SIG] How to deal with BGRA color model in PIL?

Fredrik Lundh fredrik at pythonware.com
Thu Oct 6 13:55:58 CEST 2005


"john taylor" wrote:

> i have a string of pixel values, which uses the BGRA
> color model, is there an easy way (hopefully the most
> efficient way) to convert it into the RGBA model, so
> that the image can be displayed correctly?

if you have data in a string, you can use Image.fromstring to load it into a
memory buffer, converting it on the way:

    import Image

    pixel = "\x01\x02\x03\x04"

    im = Image.fromstring("RGBA", (1, 1), pixel, "raw", "BGRA")

    print im.getpixel((0, 0)) # prints (3, 2, 1, 4)

if you have the data in an image memory in the wrong order, you can either
use split/merge (see Laszlo's reply), or roundtrip via a string buffer:

    im = ...

    new_im = Image.fromstring("RGBA", im.size, im.tostring(), "raw", "BGRA")

</F> 





More information about the Image-SIG mailing list