[Image-SIG] PIL and swapping colors

Fredrik Lundh fredrik at pythonware.com
Sun Nov 7 02:02:50 CET 2010

On Fri, Nov 5, 2010 at 7:16 AM, loz.accs <loz.accs at gmail.com> wrote:
> Hi, everyone!
> PIL doesn't provide screenshots making on linux, so I wrote
> C-extension that uses Imlib2 to grab screenshots in raw RGBA format
> and returns it to python interpreter.
> Image object (Image.fromstring) was created successfully, but red and
> blue colors was swapped.
> So my question: is there any way to swap colors in image using PIL?

You can pass in a "raw mode" argument to fromstring/frombuffer that
tells PIL what "unpacker" to use:

>>> Image.frombuffer("RGBA", (1, 1), "\1\2\3\4", "raw", "RGBA", 0, 1).getpixel((0,0))
(1, 2, 3, 4)
>>> Image.frombuffer("RGBA", (1, 1), "\1\2\3\4", "raw", "ABGR", 0, 1).getpixel((0,0))
(4, 3, 2, 1)

The default mode is the same as the image mode.

> And why don't you make linux version of ImageGrab, based on Imlib2 for example?

Lack of round tuits, mostly.  Contributions are welcome.


More information about the Image-SIG mailing list