win32 screen captures

David C. Ullrich ullrich at math.okstate.edu
Fri Aug 10 10:28:10 EDT 2001


On Fri, 10 Aug 2001 09:28:16 +0100, Robin Becker
<robin at jessikat.fsnet.co.uk> wrote:

>I saw earlier that someone had a sendkeys in python using wsh scripting
>(via win32com). I would like to do screen capture, but there seems to be
>an explicit ban on {PRTSC}.
>
>Has anyone managed to use PIL and win32com to do screen captures?

Well, I don't know how to do the Windows stuff from PIL or win32com,
but there's presumably easy ways to do it. If you want a hint what
Windows stuff to try to do: (i) You create a bitmap (presumably
this is easy in PIL, whether you can use that might depend
on whether PIL gives you access to a device context for the
bitmap). (ii) You get a device context for the screen from
GetDC(0) (iii) You BitBLT from the screen device context
to the bitmap's device context. (If this can't be done easily
using PIL and win32com it can certainly be done using
whatever.py, that Win32 API module.)

>Also the sendkeys command and control is un-robust. Is there a native
>win32com way to do this and perhaps wait on the windows/events I
>initiate before doing the capture?
>
>
>import win32api
>import win32com.client
>
>def sendkeys(k,app,delay=100):
>        '''
>        Note You can't use SendKeys to send keystrokes to an application that is not designed to run in Microsoft Windows.
>        Sendkeys also can't send the PRINT SCREEN key {PRTSC} to any application. 
>        '''
>        win32api.Sleep(delay)
>        return app.SendKeys(k)
>
>if __name__=='__main__':
>        import sys
>        shell = win32com.client.Dispatch("WScript.Shell")
>        shell.Run("Acrobat")
>        sk = lambda k, app=shell,delay=100: sendkeys(k,app,delay=delay)
>        win32api.Sleep(500)
>        shell.AppActivate("Adobe Acrobat")
>        win32api.Sleep(500)
>
>        sk('%fo',delay=500)
>        sk(sys.argv[1]+'{ENTER}',delay=500)
>        sk('%{PRTSC}',delay=1000)
>
>-- 
>Robin Becker


David C. Ullrich



More information about the Python-list mailing list