[Python-es] Como capturar la pantalla con transparencias incluidas

Josu Rodriguez Vilda jrvilda en gmail.com
Jue Oct 7 20:16:40 CEST 2010


Hola a Todos,


Necesito capturar la pantalla en Windows, solo que los intentos que he
realizado, ninguno captura las interface transparentes como por ejemplo la
ventana de buscar y reemplazar del notepad++.

He intentado estos metodos y todos funcionan para capturar pantalla pero
ninguno recoge las transparencias. ( no se ven las ventanas transparentes,
como si no existiesen)

from PIL import ImageGrab
import time

time.sleep(5)
box = (100,100,400,400)
ImageGrab.grab().crop(box).save("screen_capture.jpg", "JPEG")


*import* Image
*try*:
    # built-in driver (1.1.3 and later)    grabber =
Image.core.grabscreen*except* AttributeError:
    # stand-alone driver (pil plus)    *import* _grabscreen
    grabber = _grabscreen.grab
### (New in 1.1.3) Take a snapshot of the screen.  The pixels inside
the# bounding box are returned as an "RGB" image.  If the bounding box
is# omitted, the entire screen is copied.## @param bbox What region to
copy.  Default is the entire screen.# @return An image# @since
1.1.3*def* grab(bbox=None):
    size, data = grabber()
    im = Image.fromstring(
        "RGB", size, data,
        # RGB, 32-bit line padding, origo in lower left corner
"raw", "BGR", (size[0]*3 + 3) & -4, -1
        )
    *if* bbox:
        im = im.crop(bbox)
    *return* im
### (New in 1.1.4) Take a snapshot of the clipboard image, if any.##
@return An image, a list of filenames, or None if the clipboard does#
   not contain image data or filenames.  Note that if a list is#
returned, the filenames may not represent image files.# @since
1.1.4*def* grabclipboard():
    debug = 0 # temporary interface    data = Image.core.grabclipboard(debug)
    *if* Image.isStringType(data):
        *import* BmpImagePlugin, StringIO
        *return* BmpImagePlugin.DibImageFile(StringIO.StringIO(data))
    *return* data


import win32gui,  win32ui,  win32con, win32api



hwnd = win32gui.GetDesktopWindow()

print hwnd



# you can use this to capture only a specific window

#l, t, r, b = win32gui.GetWindowRect(hwnd)

#w = r - l

#h = b - t



# get complete virtual screen including all monitors

SM_XVIRTUALSCREEN = 76

SM_YVIRTUALSCREEN = 77

SM_CXVIRTUALSCREEN = 78

SM_CYVIRTUALSCREEN = 79

w = vscreenwidth = win32api.GetSystemMetrics(SM_CXVIRTUALSCREEN)

h = vscreenheigth = win32api.GetSystemMetrics(SM_CYVIRTUALSCREEN)

l = vscreenx = win32api.GetSystemMetrics(SM_XVIRTUALSCREEN)

t = vscreeny = win32api.GetSystemMetrics(SM_YVIRTUALSCREEN)

r = l + w

b = t + h



print l, t, r, b, ' -> ', w, h



hwndDC = win32gui.GetWindowDC(hwnd)

mfcDC  = win32ui.CreateDCFromHandle(hwndDC)

saveDC = mfcDC.CreateCompatibleDC()



saveBitMap = win32ui.CreateBitmap()

saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

saveDC.SelectObject(saveBitMap)

saveDC.BitBlt((0, 0), (w, h),  mfcDC,  (l, t),  win32con.SRCCOPY)

saveBitMap.SaveBitmapFile(saveDC,  'screencapture.bmp')






   1. import wx
   2.
   3. app = wx.PySimpleApp()
   4.
   5. context = wx.ScreenDC()
   6. r, b = context.GetSize()
   7.
   8.
   # i have a second monitor left of my primary, so these value are negativ
   9. l, t = (-1280, -256) # coulfn't find a wx function to get these
   10.
   11. w, h = (r - l, b - t)
   12. bitmap = wx.EmptyBitmap(w, h, -1)
   13.
   14. memory = wx.MemoryDC()
   15. memory.SelectObject(bitmap)
   16. memory.Blit(0, 0, w, h, context, l, t)
   17. memory.SelectObject(wx.NullBitmap)
   18.
   19. #bitmap.SaveFile("screencapture.bmp", wx.BITMAP_TYPE_BMP)
   20. #bitmap.SaveFile("screencapture.jpg", wx.BITMAP_TYPE_JPEG)
   21. bitmap.SaveFile("screencapture.png", wx.BITMAP_TYPE_PNG)



-- 
*Josu Rodriguez Vilda*
*
*
*
*
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://mail.python.org/pipermail/python-es/attachments/20101007/36c9e2e3/attachment.html>


Más información sobre la lista de distribución Python-es