Untitled

                Never    
def capture_as_image(rect=None):
    control_rectangle = self.rectangle()
    if not (control_rectangle.width() and control_rectangle.height()):
        return None

    # PIL is optional so check first
    if not ImageGrab:
        print("PIL does not seem to be installed. "
              "PIL is required for capture_as_image")
        self.actions.log("PIL does not seem to be installed. "
                         "PIL is required for capture_as_image")
        return None

    # get the control rectangle in a way that PIL likes it

    if rect:
        width = rect.width()
        height = rect.height()
        left = rect.left
        right = rect.right
        top = rect.top
        bottom = rect.bottom
    else:
        width = control_rectangle.width()
        height = control_rectangle.height()
        left = control_rectangle.left
        right = control_rectangle.right
        top = control_rectangle.top
        bottom = control_rectangle.bottom
    box = (left, top, right, bottom)

    if len(win32api.EnumDisplayMonitors()) > 1:
        temp_path = os.path.join(os.path.expandvars('%TEMP%'))
        temp_file = '{}{}screenshot_pywinauto.bmp'.format(temp_path, os.sep)
        hwin = win32gui.GetDesktopWindow()
        hwindc = win32gui.GetWindowDC(hwin)
        srcdc = win32ui.CreateDCFromHandle(hwindc)
        memdc = srcdc.CreateCompatibleDC()
        bmp = win32ui.CreateBitmap()
        bmp.CreateCompatibleBitmap(srcdc, width, height)
        memdc.SelectObject(bmp)
        memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
        bmp.SaveBitmapFile(memdc, temp_file)
        pil_img_obj = Image.open(temp_file)
    else:
        pil_img_obj = ImageGrab.grab(box)

    return pil_img_obj


if __name__ == '__main__':
    import time
    from pywinauto import Application

    app = Application().start('notepad.exe')
    notepad_window = app.top_window()
    time.sleep(0.5)
    notepad_window.set_focus()
    notepad_rectangle = notepad_window.rectangle()
    capture_as_image(notepad_rectangle).save("d:\img.png")

Raw Text