Attachment 'ScreenShotWX.py'

Download

   1 """
   2 A WX cross-platform method to capture any arbitrary portion 
   3 of the Desktop screen. Any extended screens can be captured using
   4 appropriate absolute screen coordinates.
   5 
   6 Ray Pasco
   7 pascor(at)verizon(dot)net
   8 2011-04-20-Wed__PM-04-52-36__April
   9 
  10 Adapted from "How to Take a Screenshot of Your wxPython App and Print it" @
  11 http://www.blog.pythonlibrary.org/2010/04/16/how-to-take-a-screenshot-of-your-wxpython-app-and-print-it/
  12 
  13 
  14 Revision History :
  15 
  16 2011-04-20 Debug Rev v1.2.2
  17     Using wx.Bitmap.IsOk() as the validity check for the call to wx.ScreenDC.GetAsBitmap().
  18 
  19 Platform  Linux
  20 Python    2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
  21 [GCC 4.4.5]
  22 Wx Version 2.8.11.0
  23 Wx Platform ('__WXGTK__', 'wxGTK', 'unicode', 'gtk2', 'wx-assertions-off', 'SWIG-1.3.29')
  24 
  25 2011-04-18  Debug Rev v1.2.1
  26     Added statements specifically for debugging. Enable by calling with "(... , debug=True)"
  27 
  28 2011-04-15  Rev 1.2  RDP:
  29     screenDC.GetAsBitmap() isn't implemented on MSW :(  Reversion to original Desktop bitmap 
  30     aquisition code only if scrDC.GetAsBitmap() isn't implemented. 
  31 
  32 2011-03-25  Rev. 1.1   OS-X 10.6 tested:
  33 Adapted for Mac by:
  34 Chris Barker
  35 Chris.Barker (at) noaa (dot) gov
  36 
  37 Wx Version 2.8.11.0
  38 Wx Pltform ('__WXMAC__', 'wxMac', 'unicode', 'wx-assertions-on', 'SWIG-1.3.29', 'mac-cg', 'mac-native-tb')
  39 
  40 
  41 2011-03-25  Rev. 1.0    MS Win7 tested.
  42 Ray Pasco
  43 pascor(at)verizon(dot)net
  44 
  45 Windows   6.1.7600
  46 Python    2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]
  47 Wx Version 2.8.11.0
  48 Wx Pltform ('__WXMSW__', 'wxMSW', 'unicode', 'wx-assertions-on', 'SWIG-1.3.29')
  49 
  50 """
  51 import sys
  52 import wx
  53 
  54 #------------------------------------------------------------------------------
  55 
  56 def ScreenCapture( captureStartPos, captureBmapSize, debug=False ):
  57     """
  58     General Desktop screen portion capture - partial or entire Desktop.
  59     
  60     My particular screen hardware configuration: 
  61         wx.Display( 0 ) refers to the extended Desktop display monitor screen.
  62         wx.Display( 1 ) refers to the primary  Desktop display monitor screen.
  63     
  64     Any particular Desktop screen size is :
  65         screenRect = wx.Display( n ).GetGeometry()
  66         
  67     Different wx.Display's in a single system may have different dimensions.        
  68     """
  69 
  70     # A wx.ScreenDC provides access to the entire Desktop.
  71     # This includes any extended Desktop monitor screens that are enabled in the OS.
  72     scrDC = wx.ScreenDC()
  73     scrDcSize = scrDC.Size
  74     scrDcSizeX, scrDcSizeY = scrDcSize
  75     
  76     # Cross-platform adaptations :
  77     scrDcBmap     = scrDC.GetAsBitmap()
  78     scrDcBmapSize = scrDcBmap.GetSize()
  79     if debug :
  80         print 'DEBUG:  Size of scrDC.GetAsBitmap() ', scrDcBmapSize
  81     
  82     # Check if scrDC.GetAsBitmap() method has been implemented on this platform.
  83     if   not scrDcBmap.IsOk() :   # Not implemented :  Get the screen bitmap the long way.
  84         
  85         if debug :
  86             print 'DEBUG:  Using memDC.Blit() since scrDC.GetAsBitmap() is nonfunctional.'
  87             
  88         # Create a new empty (black) destination bitmap the size of the scrDC. 
  89         # Overwrire the invalid original "scrDcBmap".
  90         scrDcBmap = wx.EmptyBitmap( *scrDcSize )
  91         scrDcBmapSizeX, scrDcBmapSizeY = scrDcSize
  92         
  93         # Create a DC tool that is associated with scrDcBmap.
  94         memDC = wx.MemoryDC( scrDcBmap )
  95         
  96         # Copy (blit, "Block Level Transfer") a portion of the screen bitmap 
  97         #   into the returned capture bitmap.
  98         # The bitmap associated with memDC (scrDcBmap) is the blit destination.
  99         
 100         memDC.Blit( 0, 0,                           # Copy to this start coordinate.
 101                     scrDcBmapSizeX, scrDcBmapSizeY, # Copy an area this size.
 102                     scrDC,                          # Copy from this DC's bitmap.
 103                     0, 0,                    )      # Copy from this start coordinate.
 104 
 105         memDC.SelectObject( wx.NullBitmap )     # Finish using this wx.MemoryDC.
 106                                                 # Release scrDcBmap for other uses.        
 107     else :
 108         
 109         if debug :
 110             print 'DEBUG:  Using scrDC.GetAsBitmap()'
 111             
 112         # This platform has scrDC.GetAsBitmap() implemented.
 113         scrDcBmap = scrDC.GetAsBitmap()     # So easy !  Copy the entire Desktop bitmap.
 114         
 115         if debug :
 116             print 'DEBUG:  scrDcBmap.GetSize() ', scrDcBmap.GetSize()
 117     
 118     #end if
 119     
 120     return scrDcBmap.GetSubBitmap( wx.RectPS( captureStartPos, captureBmapSize ) )
 121         
 122 #end ScreenCapture def

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-04-02 19:08:14, 23.2 KB) [[attachment:BitmapManip.py]]
  • [get | view] (2011-03-27 19:21:24, 1.6 KB) [[attachment:DesktopScreenShotPIL.py]]
  • [get | view] (2012-12-25 18:23:31, 8.7 KB) [[attachment:ImgConv.py]]
  • [get | view] (2011-06-06 16:22:42, 4.6 KB) [[attachment:ScreenShotWX.py]]
  • [get | view] (2011-06-06 16:22:51, 3.9 KB) [[attachment:ScreenShotWX_Demo.py]]
  • [get | view] (2011-05-16 01:34:32, 8.5 KB) [[attachment:Win32IconImagePlugin.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.