Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams Sound like what you realy want are screen videos, a la CamStudio on Sourceforge. What's the use case? Simon Hibbs Aug 27, 2010 at 16:35

You could use win32 APIs directly .

  • First give the focus to the App that you want to take screenshot of. link text

  • Win32 API can help with the screenshot:

    import win32gui
    import win32ui
    import win32con
    w = 1920 # set this
    h = 1080 # set this
    bmpfilenamename = "out.bmp" #set this
    hwnd = win32gui.FindWindow(None, windowname)
    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)
    # Free Resources
    dcObj.DeleteDC()
    cDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, wDC)
    win32gui.DeleteObject(dataBitMap.GetHandle())
                    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
                    Sep 1, 2010 at 15:39
                    As a followup from @Claudiu's comment (which is indeed necessary as he notes), I found win32gui.DeleteObject(dataBitMap.GetHandle()) also necessary.  I've edited the answer to include both comments.
    – jedwards
                    Jul 15, 2014 at 5:19
    

    You can try my newly created project DXcam: I think for raw speed it's the fastest out there (in python, and without going too deep into the rabbit hole). It's originally created for a deep learning pipeline for FPS games where the higher FPS you get the better. Plus I (am trying to) design it to be user-friendly: For a screenshot just do

    import dxcam
    camera = dxcam.create()
    frame = camera.grab()  # full screen
    frame = camera.grab(region=(left, top, right, bottom))  # region
    

    For screen capturing:

    camera.start(target_fps=60)  # threaded
    for i in range(1000):
        image = camera.get_latest_frame()  # Will block until new frame available
    camera.stop()
    

    I copied the part of the benchmarks section from the readme:

    The benchmarks is conducted through 5 trials on my 240hz monitor with a constant 240hz rendering rate synced w/the monitor (using blurbuster ufo test).

    You can read more about the details here: https://github.com/ra1nty/DXcam

    this is an interesting library! please get that on main PyPI. their testing repo is just for testing. – Christoph Rackwitz Jul 15, 2022 at 13:06 FWIW this was much slower than mss for my application, but the guarantee of every frame being new is nice. – Eric M. Sep 16, 2022 at 8:32 with mss.mss() as sct: monitor = {"top": 160, "left": 160, "width": 160, "height": 135} img_array = np.array(sct.grab(monitor)) # Do whatever you want... Ty for sharing! Using other methods at 1440p I got 13fps, but with mss I was able to achieve 20fps. It's fast for sure :) – Eric Carmichael Nov 3, 2021 at 6:07

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •