Edit detail for FrontWindowCapture revision 4 of 4

1 2 3 4
Editor: Roger Erens
Time: 2007/05/25 17:16:38 GMT-7
Note:

changed:
-This script may be useful in your documentation efforts. Comments are most welcome!
-
-# FrontWindowCapture.pyw
-# Version 0.1
-# Date    20070526
-# Author  Roger Erens (roger AT erens-krekels.net)
-"""
-        Purpose:
-        Capture the image of the front-most window into a fixed-name
-        (_front_window), max-size (default MAX_WIDTH is 640 pixels), and
-        fixed-format (PNG) file that can easily be used when creating
-        documentation for an application on a wiki or in some HTML-docs.
-        This script does _not_ copy the captured image to the clipboard (handy
-        when creating MS Word or OpenOffice documents) since MS Windows already
-        has ALT-PRTSCR to do so.
-        
-        Dependencies:
-        Fairly recent versions of Python, wxPython, and PIL should be installed
-
-        Start:
-        Double click FrontWindowCapture.pyw; an icon appears in the Task Bar (lower right of
-        the screen).
-        
-        Use:
-        Bring a window to the front and then left-click on the
-        icon in the Task Bar. A file named '_front_window.png' will be saved
-        in the current directory.
-        A pictures wider than MAXWIDTH is scaled down in order to make it easily
-        used in wikis or HTML-documentation.
-        Usually, human intelligence is needed to rename that file; this is also
-        why this script does not copy the image to the clipboard.
-
-        End:
-        Close the application by right-clicking the icon in the Task Bar.
-
-        Note:
-        Make sure that your system is fast enough to find the frontmost
-        window. That is, when a virusscanner is running or virtual memory is
-        low, this script will choke. You can see what happens by enabling the
-        print statements and running this script from a console (change the
-        extension from .pyw to .py).
-"""
-import wx
-import sys
-import win32ui
-import win32gui
-import win32con
-import time
-from PIL import Image, ImageGrab
-
-Image.init()
-
-MAX_WIDTH = 640
-BLINK_STATE = 0
-
-class TaskBarApp(wx.Frame):
-    def __init__(self, parent, id, title):
-        wx.Frame.__init__(self, parent, -1, title, size = (1, 1),
-            style=wx.FRAME_NO_TASKBAR| wx.NO_FULL_REPAINT_ON_RESIZE)
-        self.tbicon = wx.TaskBarIcon()
-        icon = wx.Icon('LeoApp16.ico', wx.BITMAP_TYPE_ICO)
-        self.tbicon.SetIcon(icon, 'Click to capture the foremost window')
-        self.tbicon.Bind(wx.EVT_TASKBAR_LEFT_UP, self.OnTaskBarLeftClick)
-        self.tbicon.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.OnTaskBarRightClick)
-        self.Show(True)
-
-    def OnTaskBarLeftClick(self, evt):
-        
-        self.FlashIcon(evt)
-        
-        def _MyCallback(hwnd, extra ):
-            extra.append(hwnd)
-
-        windows = []        
-        win32gui.EnumWindows(_MyCallback, windows)        
-
-        winList = []
-        for i in windows:
-            if win32gui.IsWindowVisible(i):
-                winList.append(i)
-                #print i
-                
-        win32gui.SetForegroundWindow(winList[1])    # found by trial and error
-                                                    # that I need the second
-                                                    # window from the list
-        time.sleep(0.1)  # give the previous command some time to finish
-
-        fgwindow = win32ui.GetForegroundWindow()
-        #print "(C)lick!"
-
-        bbox = fgwindow.GetWindowRect()
-        img = ImageGrab.grab( bbox)
-        width = bbox[2] - bbox[0]
-        if width > MAX_WIDTH:
-            coeff = MAX_WIDTH * 1. / width
-            height = bbox[3] - bbox[1]
-            newHeight = int( coeff * height)
-            #print width, height, "scaled down to", MAX_WIDTH, newHeight
-            img = img.resize((MAX_WIDTH,newHeight), Image.ANTIALIAS)
-        img = img.convert("P", dither=Image.NONE,
-                              palette=Image.ADAPTIVE, colors=256)
-        img.save("_front_window.png", "PNG", optimize=1)
-        #img.show() # show the image in an image viewer
-            
-        self.FlashIcon(evt)
-        
-    def OnTaskBarRightClick(self, evt):
-        #print "Auta..." # that's about it...
-        self.tbicon.Destroy()
-        self.Close(True)
-        wx.GetApp().ProcessIdle()
-
-    def FlashIcon(self, evt):
-        global BLINK_STATE
-        if BLINK_STATE == 0:
-            icon = wx.Icon('LeoApp16lick.ico', wx.BITMAP_TYPE_ICO)
-            self.tbicon.SetIcon(icon, 'Window is being captured...')
-            BLINK_STATE = 1
-            return
-        else:
-            icon = wx.Icon('LeoApp16.ico', wx.BITMAP_TYPE_ICO)
-            self.tbicon.SetIcon(icon, 'Click to capture the foremost window')
-            BLINK_STATE = 0
-            return
-
-class MyApp(wx.App):
-    def OnInit(self):
-        frame = TaskBarApp(None, -1, ' ')
-        frame.Center(wx.BOTH)
-        frame.Show(False)
-        return True
-
-def main():
-    app = MyApp(0)
-    app.MainLoop()
-
-if __name__ == '__main__':
-    main()
-
-""" CREDITS
-    
-    Thanks to
-    Roger Upole:
-    http://groups.google.com/group/comp.lang.python/
-    browse_thread/thread/ff39ec79f7c3248a/
-    e600c892772bf52a?lnk=gst&q=screen+capture&rnum=6#e600c892772bf52a
-    for his time.sleep() Aha (This took me a year and a half to note!)
-
-    Christian Wyglendowski, Werner Bruhin, and Xavier Morel for the
-    http://wiki.wxpython.org/index.cgi/FlashingTaskbarIcon
-    discussion
-
-    'Rob aspn at msolutionsinc.com '
-    http://mail.python.org/pipermail/python-win32/2003-June/001129.html
-    for his suggestion to find out the foremost window
-
-    and of course the usual suspects that created/maintain
-    wxPython,
-    PIL,
-    pywin32,
-    LEO,
-    gvim,
-    and Python itself.
-"""
-
-
-
This script may be useful in your documentation efforts. Comments are most welcome::

        # FrontWindowCapture.pyw
        # Version 0.1
        # Date    20070526
        # Author  Roger Erens (roger AT erens-krekels.net)
        """
                Purpose:
                Capture the image of the front-most window into a fixed-name
                (_front_window), max-size (default MAX_WIDTH is 640 pixels), and
                fixed-format (PNG) file that can easily be used when creating
                documentation for an application on a wiki or in some HTML-docs.
                This script does _not_ copy the captured image to the clipboard (handy
                when creating MS Word or OpenOffice documents) since MS Windows already
                has ALT-PRTSCR to do so.
                
                Dependencies:
                Fairly recent versions of Python, wxPython, and PIL should be installed
        
                Start:
                Double click FrontWindowCapture.pyw; an icon appears in the Task Bar (lower right of
                the screen).
                
                Use:
                Bring a window to the front and then left-click on the
                icon in the Task Bar. A file named '_front_window.png' will be saved
                in the current directory.
                A pictures wider than MAXWIDTH is scaled down in order to make it easily
                used in wikis or HTML-documentation.
                Usually, human intelligence is needed to rename that file; this is also
                why this script does not copy the image to the clipboard.
        
                End:
                Close the application by right-clicking the icon in the Task Bar.
        
                Note:
                Make sure that your system is fast enough to find the frontmost
                window. That is, when a virusscanner is running or virtual memory is
                low, this script will choke. You can see what happens by enabling the
                print statements and running this script from a console (change the
                extension from .pyw to .py).
        """
        import wx
        import sys
        import win32ui
        import win32gui
        import win32con
        import time
        from PIL import Image, ImageGrab
        
        Image.init()
        
        MAX_WIDTH = 640
        BLINK_STATE = 0
        
        class TaskBarApp(wx.Frame):
            def __init__(self, parent, id, title):
                wx.Frame.__init__(self, parent, -1, title, size = (1, 1),
                    style=wx.FRAME_NO_TASKBAR| wx.NO_FULL_REPAINT_ON_RESIZE)
                self.tbicon = wx.TaskBarIcon()
                icon = wx.Icon('LeoApp16.ico', wx.BITMAP_TYPE_ICO)
                self.tbicon.SetIcon(icon, 'Click to capture the foremost window')
                self.tbicon.Bind(wx.EVT_TASKBAR_LEFT_UP, self.OnTaskBarLeftClick)
                self.tbicon.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.OnTaskBarRightClick)
                self.Show(True)
        
            def OnTaskBarLeftClick(self, evt):
                
                self.FlashIcon(evt)
                
                def _MyCallback(hwnd, extra ):
                    extra.append(hwnd)
        
                windows = []        
                win32gui.EnumWindows(_MyCallback, windows)        
        
                winList = []
                for i in windows:
                    if win32gui.IsWindowVisible(i):
                        winList.append(i)
                        #print i
                        
                win32gui.SetForegroundWindow(winList[1])    # found by trial and error
                                                            # that I need the second
                                                            # window from the list
                time.sleep(0.1)  # give the previous command some time to finish
        
                fgwindow = win32ui.GetForegroundWindow()
                #print "(C)lick!"
        
                bbox = fgwindow.GetWindowRect()
                img = ImageGrab.grab( bbox)
                width = bbox[2] - bbox[0]
                if width > MAX_WIDTH:
                    coeff = MAX_WIDTH * 1. / width
                    height = bbox[3] - bbox[1]
                    newHeight = int( coeff * height)
                    #print width, height, "scaled down to", MAX_WIDTH, newHeight
                    img = img.resize((MAX_WIDTH,newHeight), Image.ANTIALIAS)
                img = img.convert("P", dither=Image.NONE,
                                      palette=Image.ADAPTIVE, colors=256)
                img.save("_front_window.png", "PNG", optimize=1)
                #img.show() # show the image in an image viewer
                    
                self.FlashIcon(evt)
                
            def OnTaskBarRightClick(self, evt):
                #print "Auta..." # that's about it...
                self.tbicon.Destroy()
                self.Close(True)
                wx.GetApp().ProcessIdle()
        
            def FlashIcon(self, evt):
                global BLINK_STATE
                if BLINK_STATE == 0:
                    icon = wx.Icon('LeoApp16lick.ico', wx.BITMAP_TYPE_ICO)
                    self.tbicon.SetIcon(icon, 'Window is being captured...')
                    BLINK_STATE = 1
                    return
                else:
                    icon = wx.Icon('LeoApp16.ico', wx.BITMAP_TYPE_ICO)
                    self.tbicon.SetIcon(icon, 'Click to capture the foremost window')
                    BLINK_STATE = 0
                    return
        
        class MyApp(wx.App):
            def OnInit(self):
                frame = TaskBarApp(None, -1, ' ')
                frame.Center(wx.BOTH)
                frame.Show(False)
                return True
        
        def main():
            app = MyApp(0)
            app.MainLoop()
        
        if __name__ == '__main__':
            main()
        
        """ CREDITS
            
            Thanks to
            Roger Upole:
            http://groups.google.com/group/comp.lang.python/
            browse_thread/thread/ff39ec79f7c3248a/
            e600c892772bf52a?lnk=gst&q=screen+capture&rnum=6#e600c892772bf52a
            for his time.sleep() Aha (This took me a year and a half to note!)
        
            Christian Wyglendowski, Werner Bruhin, and Xavier Morel for the
            http://wiki.wxpython.org/index.cgi/FlashingTaskbarIcon
            discussion
        
            'Rob aspn at msolutionsinc.com '
            http://mail.python.org/pipermail/python-win32/2003-June/001129.html
            for his suggestion to find out the foremost window
        
            and of course the usual suspects that created/maintain
            wxPython,
            PIL,
            pywin32,
            LEO,
            gvim,
            and Python itself.
        """

This script may be useful in your documentation efforts. Comments are most welcome:

# FrontWindowCapture.pyw
# Version 0.1
# Date    20070526
# Author  Roger Erens (roger AT erens-krekels.net)
"""
        Purpose:
        Capture the image of the front-most window into a fixed-name
        (_front_window), max-size (default MAX_WIDTH is 640 pixels), and
        fixed-format (PNG) file that can easily be used when creating
        documentation for an application on a wiki or in some HTML-docs.
        This script does _not_ copy the captured image to the clipboard (handy
        when creating MS Word or OpenOffice documents) since MS Windows already
        has ALT-PRTSCR to do so.

        Dependencies:
        Fairly recent versions of Python, wxPython, and PIL should be installed

        Start:
        Double click FrontWindowCapture.pyw; an icon appears in the Task Bar (lower right of
        the screen).

        Use:
        Bring a window to the front and then left-click on the
        icon in the Task Bar. A file named '_front_window.png' will be saved
        in the current directory.
        A pictures wider than MAXWIDTH is scaled down in order to make it easily
        used in wikis or HTML-documentation.
        Usually, human intelligence is needed to rename that file; this is also
        why this script does not copy the image to the clipboard.

        End:
        Close the application by right-clicking the icon in the Task Bar.

        Note:
        Make sure that your system is fast enough to find the frontmost
        window. That is, when a virusscanner is running or virtual memory is
        low, this script will choke. You can see what happens by enabling the
        print statements and running this script from a console (change the
        extension from .pyw to .py).
"""
import wx
import sys
import win32ui
import win32gui
import win32con
import time
from PIL import Image, ImageGrab

Image.init()

MAX_WIDTH = 640
BLINK_STATE = 0

class TaskBarApp(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, -1, title, size = (1, 1),
            style=wx.FRAME_NO_TASKBAR| wx.NO_FULL_REPAINT_ON_RESIZE)
        self.tbicon = wx.TaskBarIcon()
        icon = wx.Icon('LeoApp16.ico', wx.BITMAP_TYPE_ICO)
        self.tbicon.SetIcon(icon, 'Click to capture the foremost window')
        self.tbicon.Bind(wx.EVT_TASKBAR_LEFT_UP, self.OnTaskBarLeftClick)
        self.tbicon.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.OnTaskBarRightClick)
        self.Show(True)

    def OnTaskBarLeftClick(self, evt):

        self.FlashIcon(evt)

        def _MyCallback(hwnd, extra ):
            extra.append(hwnd)

        windows = []
        win32gui.EnumWindows(_MyCallback, windows)

        winList = []
        for i in windows:
            if win32gui.IsWindowVisible(i):
                winList.append(i)
                #print i

        win32gui.SetForegroundWindow(winList[1])    # found by trial and error
                                                    # that I need the second
                                                    # window from the list
        time.sleep(0.1)  # give the previous command some time to finish

        fgwindow = win32ui.GetForegroundWindow()
        #print "(C)lick!"

        bbox = fgwindow.GetWindowRect()
        img = ImageGrab.grab( bbox)
        width = bbox[2] - bbox[0]
        if width > MAX_WIDTH:
            coeff = MAX_WIDTH * 1. / width
            height = bbox[3] - bbox[1]
            newHeight = int( coeff * height)
            #print width, height, "scaled down to", MAX_WIDTH, newHeight
            img = img.resize((MAX_WIDTH,newHeight), Image.ANTIALIAS)
        img = img.convert("P", dither=Image.NONE,
                              palette=Image.ADAPTIVE, colors=256)
        img.save("_front_window.png", "PNG", optimize=1)
        #img.show() # show the image in an image viewer

        self.FlashIcon(evt)

    def OnTaskBarRightClick(self, evt):
        #print "Auta..." # that's about it...
        self.tbicon.Destroy()
        self.Close(True)
        wx.GetApp().ProcessIdle()

    def FlashIcon(self, evt):
        global BLINK_STATE
        if BLINK_STATE == 0:
            icon = wx.Icon('LeoApp16lick.ico', wx.BITMAP_TYPE_ICO)
            self.tbicon.SetIcon(icon, 'Window is being captured...')
            BLINK_STATE = 1
            return
        else:
            icon = wx.Icon('LeoApp16.ico', wx.BITMAP_TYPE_ICO)
            self.tbicon.SetIcon(icon, 'Click to capture the foremost window')
            BLINK_STATE = 0
            return

class MyApp(wx.App):
    def OnInit(self):
        frame = TaskBarApp(None, -1, ' ')
        frame.Center(wx.BOTH)
        frame.Show(False)
        return True

def main():
    app = MyApp(0)
    app.MainLoop()

if __name__ == '__main__':
    main()

""" CREDITS

    Thanks to
    Roger Upole:
    http://groups.google.com/group/comp.lang.python/
    browse_thread/thread/ff39ec79f7c3248a/
    e600c892772bf52a?lnk=gst&q=screen+capture&rnum=6#e600c892772bf52a
    for his time.sleep() Aha (This took me a year and a half to note!)

    Christian Wyglendowski, Werner Bruhin, and Xavier Morel for the
    http://wiki.wxpython.org/index.cgi/FlashingTaskbarIcon
    discussion

    'Rob aspn at msolutionsinc.com '
    http://mail.python.org/pipermail/python-win32/2003-June/001129.html
    for his suggestion to find out the foremost window

    and of course the usual suspects that created/maintain
    wxPython,
    PIL,
    pywin32,
    LEO,
    gvim,
    and Python itself.
"""

image: http://leo.zwiki.org/FrontWindowCapture/LeoApp16.ico

image: http://leo.zwiki.org/FrontWindowCapture/LeoApp16lick.ico