Grab screen in wxpython

Hi all
Is there a way to grab an image from the screen using wxpython?
I know that it could be done using PIL ImageGrab.grab(bbox) => image but unfortunately it is only for windows.

TIA

G

Hi Giovanni,

Is there a way to grab an image from the screen using wxpython?

If you mean taking a screenshot of one widget or one part of the application, you may do something like:

def TakeScreenShot(rect):
“”" Takes a screenshot of the screen at give pos & size (rect). “”"

#Create a DC for the whole screen area
dcScreen = wx.ScreenDC()

#Create a Bitmap that will later on hold the screenshot image
#Note that the Bitmap must have a size big enough to hold the screenshot
#-1 means using the current default colour depth
bmp = wx.EmptyBitmap(rect.width, rect.height)

#Create a memory DC that will be used for actually taking the screenshot
memDC = wx.MemoryDC()

#Tell the memory DC to use our Bitmap
#all drawing action on the memory DC will go to the Bitmap now
memDC.SelectObject(bmp)

#Blit (in this case copy) the actual screen on the memory DC
#and thus the Bitmap
memDC.Blit( 0, #Copy to this X coordinate
    0, #Copy to this Y coordinate
    rect.width, #Copy this width
    rect.height, #Copy this height
    dcScreen, #From where do we copy?
    rect.x, #What's the X offset in the original DC?
    rect.y  #What's the Y offset in the original DC?
    )

#Select the Bitmap out of the memory DC by selecting a new
#uninitialized Bitmap
memDC.SelectObject(wx.NullBitmap)

return bmp

HTH.

Andrea.

···

Andrea Gavana
(gavana@kpo.kz)

Reservoir Engineer

KPDL

4, Millbank

SW1P 3JA London

Direct Tel: +44 (0) 20 717 08936

Mobile Tel: +44 (0) 77 487 70534

Fax: +44 (0) 20 717 08900

Web:
http://xoomer.virgilio.it/infinity77

¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯


From: Giovanni Porcari [mailto:giovanni.porcari@gmail.com]
Sent: 08 November 2006 13:31
To: wxpython-users
Subject: [wxPython-users] Grab screen in wxpython

Hi all
Is there a way to grab an image from the screen using wxpython?
I know that it could be done using PIL ImageGrab.grab(bbox) => image but unfortunately it is only for windows.

TIA

G

Thank you Andrea

:slight_smile:

G

···

Il giorno 08/nov/06, alle ore 14:38, Gavana, Andrea ha scritto:

Hi Giovanni,

Is there a way to grab an image from the screen using wxpython?

If you mean taking a screenshot of one widget or one part of the application, you may do something like:

def TakeScreenShot(rect):
“”" Takes a screenshot of the screen at give pos & size (rect). “”"

#Create a DC for the whole screen area
dcScreen = wx.ScreenDC()
#Create a Bitmap that will later on hold the screenshot image
#Note that the Bitmap must have a size big enough to hold the screenshot
#-1 means using the current default colour depth
bmp = wx.EmptyBitmap(rect.width, rect.height)
#Create a memory DC that will be used for actually taking the screenshot
memDC = wx.MemoryDC()
#Tell the memory DC to use our Bitmap
#all drawing action on the memory DC will go to the Bitmap now
memDC.SelectObject(bmp)
#Blit (in this case copy) the actual screen on the memory DC
#and thus the Bitmap
memDC.Blit( 0, #Copy to this X coordinate
    0, #Copy to this Y coordinate
    rect.width, #Copy this width
    rect.height, #Copy this height
    dcScreen, #From where do we copy?
    rect.x, #What's the X offset in the original DC?
    rect.y  #What's the Y offset in the original DC?
    )
#Select the Bitmap out of the memory DC by selecting a new
#uninitialized Bitmap
memDC.SelectObject(wx.NullBitmap)
return bmp

That is very nifty.

If one uses the print screen button on Windows, then one can also use
something like the following...

def GetClipboardImage():
    success = False
    do = wx.BitmapDataObject()
    if wx.TheClipboard.Open():
        success = wx.TheClipboard.GetData(do)
        wx.TheClipboard.Close()

    if success:
        return do.GetBitmap()
    return None

- Josiah

···

"Gavana, Andrea" <gavana@kpo.kz> wrote:

Hi Giovanni,

> Is there a way to grab an image from the screen using wxpython?

If you mean taking a screenshot of one widget or one part of the application, you may do something like:

def TakeScreenShot(rect):
    """ Takes a screenshot of the screen at give pos & size (rect). """

    #Create a DC for the whole screen area
    dcScreen = wx.ScreenDC()

    #Create a Bitmap that will later on hold the screenshot image
    #Note that the Bitmap must have a size big enough to hold the screenshot
    #-1 means using the current default colour depth
    bmp = wx.EmptyBitmap(rect.width, rect.height)

    #Create a memory DC that will be used for actually taking the screenshot
    memDC = wx.MemoryDC()

    #Tell the memory DC to use our Bitmap
    #all drawing action on the memory DC will go to the Bitmap now
    memDC.SelectObject(bmp)

    #Blit (in this case copy) the actual screen on the memory DC
    #and thus the Bitmap
    memDC.Blit( 0, #Copy to this X coordinate
        0, #Copy to this Y coordinate
        rect.width, #Copy this width
        rect.height, #Copy this height
        dcScreen, #From where do we copy?
        rect.x, #What's the X offset in the original DC?
        rect.y #What's the Y offset in the original DC?
        )

    #Select the Bitmap out of the memory DC by selecting a new
    #uninitialized Bitmap
    memDC.SelectObject(wx.NullBitmap)

    return bmp