What's the fastest way to take a screenshot on windows? PIL.ImageGrab
is rather slow.. it takes between 4-5 seconds to take 30 screenshots of the same small window. Taking screenshots of the whole desktop is even slower.
views:
88answers:
2
+5
A:
You could use win32 APIs directly .
1) First give the focus to the App that you want to take screenshot of. link text
2) Win32 API can help with the screenshot:
wDC = win32gui.GetWindowDC(hwnd)
dcObj=win32ui.CreateDCFromHandle(wDC)
cDC=dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0,0),(w, h) , dcObj, (0,0), win32con.SRCCOPY)
dataBitMap.SaveBitmapFile(cDC, bmpfilenamename)
pyfunc
2010-08-27 16:33:00
VERY IMPORTANT, as this recently bit me in the ass: you HAVE TO delete/release all the DCs created by this, or after taking ~90 images, you won't be able to anymore. in this case, `dcObj.DeleteDC(); cDC.DeleteDC(); win32gui.ReleaseDC(hwnd, wDC)`
Claudiu
2010-09-01 15:39:16
A:
Just found out how to do it with gtk. Seems fastest by far:
def image_grab_gtk(window):
left, top, right, bot = get_rect(window)
w = right - left
h = bot - top
s = gtk.gdk.Pixbuf(
gtk.gdk.COLORSPACE_RGB, False, 8, w, h)
s.get_from_drawable(
gtk.gdk.get_default_root_window(),
gtk.gdk.colormap_get_system(),
left, top, 0, 0, w, h )
final = Image.frombuffer(
"RGB",
(w, h),
s.get_pixels(),
"raw",
"RGB",
s.get_rowstride(), 1)
return final
Without converting to a PIL
Image, it's 8x faster than PIL on my test case. With converting, it's still ~2.7x faster.
Claudiu
2010-08-27 22:37:34