Converting a PIL image object to a buffer

S

Simon Hibbs

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.

Help appreciated,

Simon Hibbs
 
G

Gary Herron

Simon said:
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.

Gary Herron
 
S

Simon Hibbs

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
 
D

Diez B. Roggisch

Simon said:
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.

Did you actually try that? Strings support the buffer interface, and the
type of the binary data you set should be defined by the first argument.

Alternatively (if the string is not of the proper format), maybe storing
the image to a (c)StringIO-object as BMP and retrieving it's value would
help.

However, I think your concerns about "wasting memory" when using a file
are moot - creating an extra memory buffer isn't less memory consuming,
and if the file is living only a few seconds it might not even actually
hit the disk at all. In the end, the important thing is the working
clipboard.

Diez
 
S

Simon Hibbs

Simon Hibbs schrieb:





Did you actually try that? Strings support the buffer interface, and the
type of the binary data you set should be defined by the first argument.

Alternatively (if the string is not of the proper format), maybe storing
the image to a (c)StringIO-object as BMP and retrieving it's value would
help.

However, I think your concerns about "wasting memory" when using a file
are moot - creating an extra memory buffer isn't less memory consuming,
and if the file is living only a few seconds it might not even actually
hit the disk at all. In the end, the important thing is the working
clipboard.

Yes I did try this, the code to dump the sting version to the
clipboard worked, but pasting it back out wasn't possible although I
could print them to the console.

I don't mean wasting memory, just that actualy hitting the file system
and creating a file seems as though it shouldn't be necessery. Is
there any good reason why you can't just create an empty buffer object
or file object directly? It seems like an unecessery restriction,
unless there's some underlying reason such as that it's hard to
implement in c, which seems implausible. Sorry, that's a rhetorical
question.

I've not had a chance to work on this today, if I find an elegant way
round it I'll post the results for future googling.

Simon Hibbs
 
G

Gabriel Genellina

The second argument to SetClipboardData should be a handle to a bitmap
resource, not a string.
See win32\test\test_clipboard.py for an example.

PS: Hmm, looking at SetClipboardData, seems that a string containing the
data in the right format *might* work too. But it's easier to use
LoadImage than building the resource by hand, I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top