Converting a PIL image object to a buffer

Simon Hibbs simon.hibbs at gmail.com
Wed Apr 1 18:20:43 EDT 2009


On 1 Apr, 21:43, Gary Herron <gher... at islandtraining.com> wrote:
> Simon Hibbs wrote:
> > I'm trying to dump a snapshot of my application window to the
> > clipboard. I can use ImageGrab in PIL to get the screen data into a
> > PIL image object, which i have converted to a bitmap using ImageWin,
> > but when I try to pass this to the clipboard using -
>
> > win32clipboard.SetClipboardData(win32clipboard.CF_BITMAP, img)
>
> > It fails, telling be that "The object must support the buffer
> > interface".
>
> > How can I convert a PIL image into a buffer object? I can't find any
> > clues.
>
> PIL images have a tostring method that returns a string containing all
> the pixel data.  Would that help you to either create the needed
> buffer?  Or perhaps you could by-pass the need for a buffer, and just
> use the byte string.

If I use tostring I get a string which I can put on the clipboard, but
it isn't any kind of image. I can make a PIL image from the string but
them I'm back to square one again.

I suppse I could save the image object to a real file and then send
that to the clipboard, but that seems wasteful and I'd have to worry
about what to call it and where to put it. Much neater if I could just
create it in memory somehow.

Simon Hibbs



More information about the Python-list mailing list