How to save bitmap from MS Windows clipboard to xxx.png?

Kevin Altis altis at semi-retired.com
Fri Apr 19 17:28:00 EDT 2002


There are two issues here. One is getting the contents of the clipboard. The
second is taking a bitmap in memory and then saving it as a PNG file. I just
tried this snippet in wxPython which will work in Windows, Linux, etc.

from wxPython import wx

...

        try:
            if wx.wxTheClipboard.Open():
                if not
wx.wxTheClipboard.IsSupported(wx.wxDataFormat(wx.wxDF_BITMAP)):
                    return
                do = wx.wxBitmapDataObject()
                wx.wxTheClipboard.GetData(do)
                bmp = do.GetBitmap()
                bmp.SaveFile('clipboard.png', wx.wxBITMAP_TYPE_PNG)
                wx.wxTheClipboard.Close()
        except:
            pass

This is not a complete app and could be better about handling the possible
exceptions, but that's the basic idea. There is something like this included
in the doodle sample in PythonCard, except that it pastes the image into the
on screen window instead of saving the bitmap to disk.

wxPython home page
http://www.wxpython.org/

PythonCard home page
http://pythoncard.sourceforge.net/

ka

"Gillou" <nospam at bigfoot.com> wrote in message
news:a9pb8b$2nlg$1 at norfair.nerim.net...
> Hi,
>
> Have a look to the Python Imaging Library (AKA PIL). This should be a good
> start for converting graphic formats
>
> http://www.pythonware.com/products/pil/index.htm
>
> And have a look to the Python Win32 extensions that may (?) provide
> scripting access to the clipboard.
>
> http://starship.python.net/crew/mhammond/
>
> HTH
>
> --Gilles
>
> "Petr Prikryl" <Answer.via.news.please.prikryl at skil.nospam.cz> a écrit
dans
> le message news: a9p1e3$efb$1 at news.nextra.cz...
> > Hi,
> >
> > I am quite new to Python to now what was done already
> > and what was not.
> >
> > What I need is to save the window snapshot (in MS
> > Windows) into PNG file.  It just came to my mind that
> > some Python utility may already exist for that task
> > or that some PNG support is implemented.
> >
> [...]
>
>





More information about the Python-list mailing list