def grab_screen(region=None):
    hwin = win32gui.GetDesktopWindow()
    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    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)

    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
Example #2
0
def window_array(hwnd: int) -> Image:
    """윈도우 이미지를 배열 형태로 가져옴

    Args:
        hwnd: 윈도우 핸들
    
    Return:
        윈도우 이미지 배열
    """
    rect: Rect = window_rect(hwnd)
    window_dc = win32gui.GetWindowDC(hwnd)
    dc_hwnd = win32ui.CreateDCFromHandle(window_dc)
    new_dc = dc_hwnd.CreateCompatibleDC()
    # bitmap object create & select
    bitmap = win32ui.CreateBitmap()
    bitmap.CreateCompatibleBitmap(dc_hwnd, rect.width, rect.height)
    new_dc.SelectObject(bitmap)
    # capturing window
    windll.user32.PrintWindow(hwnd, new_dc.GetSafeHdc(), 0x2)
    # formating image
    bitmap_array = numpy.asarray(bitmap.GetBitmapBits(), dtype='uint8')

    bmp_pil = pil_Image.frombuffer('RGB', (rect.width, rect.height),
                                   bitmap_array, 'raw', 'BGRX', 0, 1)
    img = numpy.array(bmp_pil)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    # close DC
    win32gui.DeleteObject(bitmap.GetHandle())
    dc_hwnd.DeleteDC()
    new_dc.DeleteDC()
    win32gui.ReleaseDC(hwnd, window_dc)
    return Image(img)
Example #3
0
def screengrab(hwnd):
    import win32gui
    import win32ui
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    width, height = right - left, bottom - top
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveDC.SetWindowOrg((0, 0))  # 13, 151))
    saveBitMap = win32ui.CreateBitmap()

    saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)  # 287, 76)
    saveDC.SelectObject(saveBitMap)
    #    result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
    import win32con
    win32gui.BitBlt(saveDC.GetSafeHdc(), 0, 0, width, height, hwndDC, 0, 0,
                    win32con.SRCCOPY)

    bmpinfo = saveBitMap.GetInfo()
    bmpstr = saveBitMap.GetBitmapBits(True)
    im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
                          bmpstr, 'raw', 'BGRX', 0, 1)

    win32gui.DeleteObject(saveBitMap.GetHandle())
    saveDC.DeleteDC()
    mfcDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)
    return im
def screenshot(widget):
    path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'screenshot.png')
    if _WIN32:
        targetWindow = win32gui.FindWindow(None, APP_NAME)
        left, top, right, bot = win32gui.GetClientRect(targetWindow)
        w = right - left
        h = bot - top

        hwndDC = win32gui.GetWindowDC(targetWindow)
        mfcDC  = win32ui.CreateDCFromHandle(hwndDC)
        saveDC = mfcDC.CreateCompatibleDC()

        saveBitMap = win32ui.CreateBitmap()
        saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

        saveDC.SelectObject(saveBitMap)

        result = windll.user32.PrintWindow(targetWindow, saveDC.GetSafeHdc(), 1)

        if result != 0:
            #bmpinfo = saveBitMap.GetInfo()
            bmpstr = saveBitMap.GetBitmapBits(True)

            img = Image.frombuffer('RGB', (w, h),
                bmpstr, 'raw', 'BGRX', 0, 1)

            img.save(path)
        
            #img = img.convert("RGB")
            #data = img.tobytes("raw","RGB")

            #converting to QImage with altering window size causes crash / erroneous images
            #qimg = QImage(data, img.size[0], img.size[1], QImage.Format_RGB888)

            #qimg.save(path)
        else:
            print("Win32 failed, backup qt function used")
            widget.screenCap = widget.screen().grabWindow(widget.long_qdesktop_id,
                        widget.geometry().x(), widget.geometry().y(),
                        widget.width(), widget.height()).save(path, 'png')

        win32gui.DeleteObject(saveBitMap.GetHandle())
        saveDC.DeleteDC()
        mfcDC.DeleteDC()
        win32gui.ReleaseDC(targetWindow, hwndDC)
    else:
        widget.screenCap = widget.screen().grabWindow(widget.long_qdesktop_id,
                                    widget.geometry().x(), widget.geometry().y(),
                                    widget.width(), widget.height()).save(path, 'png')
Example #5
0
 def get_screenshot(self):
     wDC = win32gui.GetWindowDC(self.hwnd)
     dcObj = win32ui.CreateDCFromHandle(wDC)
     cDC = dcObj.CreateCompatibleDC()
     dataBitMap = win32ui.CreateBitmap()
     dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
     cDC.SelectObject(dataBitMap)
     cDC.BitBlt((0, 0), (self.w, self.h), dcObj,
                (self.cropped_x, self.cropped_y), win32con.SRCCOPY)
     signedIntsArray = dataBitMap.GetBitmapBits(True)
     # img = np.fromstring(signedIntsArray, dtype='uint8')
     img = np.frombuffer(signedIntsArray, dtype='uint8')
     img.shape = (self.h, self.w, 4)
     dcObj.DeleteDC()
     cDC.DeleteDC()
     win32gui.ReleaseDC(self.hwnd, wDC)
     win32gui.DeleteObject(dataBitMap.GetHandle())
     img = img[..., :3]
     img = np.ascontiguousarray(img)
     return img
Example #6
0
def capture_screen(hwnd, w, h):
    # https://stackoverflow.com/questions/19695214/python-screenshot-of-inactive-window-printwindow-win32gui
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)
    result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
    bmpinfo = saveBitMap.GetInfo()
    bmpstr = saveBitMap.GetBitmapBits(True)
    im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
                          bmpstr, 'raw', 'BGRX', 0, 1)

    win32gui.DeleteObject(saveBitMap.GetHandle())
    saveDC.DeleteDC()
    mfcDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)

    if result == 1:
        return im
    return None
Example #7
0
 def screenshot(self, output='screen_shot.png'):
     window_title = self._getWindowTitle()
     width, height = constants.BASE_WIDTH, constants.BASE_HEIGHT
     try:
         hwin = win32gui.FindWindow('LDPlayerMainFrame', window_title)
         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,
                      (0, self._getDnToolbarHeight()), win32con.SRCCOPY)
         signedIntsArray = bmp.GetBitmapBits(True)
         img = np.frombuffer(signedIntsArray, dtype='uint8')
         img.shape = (height, width, 4)
         srcdc.DeleteDC()
         memdc.DeleteDC()
         win32gui.ReleaseDC(hwin, hwindc)
         win32gui.DeleteObject(bmp.GetHandle())
         return img[:, :, :3]
     except:
         return super().screenshot(output=output)
    def captureWindow(self):
        if _WIN32:
            targetWindow = win32gui.FindWindow(None, 'Pokémon Tower Battle')
            left, top, right, bot = win32gui.GetClientRect(targetWindow)
            w = right - left
            h = bot - top

            hwndDC = win32gui.GetWindowDC(targetWindow)
            mfcDC  = win32ui.CreateDCFromHandle(hwndDC)
            saveDC = mfcDC.CreateCompatibleDC()

            saveBitMap = win32ui.CreateBitmap()
            saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

            saveDC.SelectObject(saveBitMap)

            result = windll.user32.PrintWindow(targetWindow, saveDC.GetSafeHdc(), 1)

            if result != 0:
                bmpstr = saveBitMap.GetBitmapBits(True)

                self.screenCap = Image.frombuffer('RGB', (w, h), bmpstr, 'raw', 'BGRX', 0, 1)
            else:
                #backup function if windows fails
                self.screenCap = self.screen().grabWindow(self.long_qdesktop_id,
                            self.geometry().x(), self.geometry().y(),
                            self.width(), self.height()).toImage()

            win32gui.DeleteObject(saveBitMap.GetHandle())
            saveDC.DeleteDC()
            mfcDC.DeleteDC()
            win32gui.ReleaseDC(targetWindow, hwndDC)
        else:
            #must have screen on top
            self.screenCap = self.screen().grabWindow(self.long_qdesktop_id,
                                       self.geometry().x(), self.geometry().y(),
                                       self.width(), self.height()).toImage()
Example #9
0
def __get_image(hwnd: int, rect: RECT, print_window: bool = False):
    width: LONG = rect.right - rect.left
    height: LONG = rect.bottom - rect.top
    # If the window closes while it's being manipulated, it could cause a crash
    try:
        windowDC: int = win32gui.GetWindowDC(hwnd)
        dcObject = win32ui.CreateDCFromHandle(windowDC)

        # Causes a 10-15x performance drop. But allows recording hardware accelerated windows
        if (print_window):
            windll.user32.PrintWindow(hwnd, dcObject.GetSafeHdc(),
                                      PW_RENDERFULLCONTENT)

        compatibleDC = dcObject.CreateCompatibleDC()
        bitmap: HBITMAP = win32ui.CreateBitmap()
        bitmap.CreateCompatibleBitmap(dcObject, width, height)
        compatibleDC.SelectObject(bitmap)
        compatibleDC.BitBlt((0, 0), (width, height), dcObject,
                            (rect.left, rect.top), win32con.SRCCOPY)
    except (win32ui.error, pywintypes.error):
        errorImage = np.array([0, 0, 0, 1], dtype="uint8")
        return errorImage

    image: np._BufferType = np.frombuffer(bitmap.GetBitmapBits(True),
                                          dtype='uint8')
    image.shape = (height, width, 4)

    try:
        dcObject.DeleteDC()
        compatibleDC.DeleteDC()
        win32gui.ReleaseDC(hwnd, windowDC)
        win32gui.DeleteObject(bitmap.GetHandle())
    except win32ui.error:
        pass

    return image
Example #10
0
    def GetScreenImg(self):
        if self.hwnd is None:
            raise Exception(
                "HWND is none. HWND not called or invalid window name provided."
            )
        l, t, r, b = win32gui.GetWindowRect(self.hwnd)
        self.winLeft, self.winTop, self.winRight, self.winBottom = win32gui.GetClientRect(
            self.hwnd)
        # Calculate Width of Window
        w = self.winRight - self.winLeft
        # Calculate Height of Window
        h = self.winBottom - self.winTop

        wDC = win32gui.GetWindowDC(self.hwnd)
        dcObj = win32ui.CreateDCFromHandle(wDC)
        cDC = dcObj.CreateCompatibleDC()
        dataBitMap = win32ui.CreateBitmap()
        dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
        cDC.SelectObject(dataBitMap)
        #First 2 tuples are top-left and bottom-right of destination
        #Third tuple is the start position in source
        cDC.BitBlt((0, 0), (w, h), dcObj, (0, 0), win32con.SRCCOPY)
        #result = windll.user32.PrintWindow(self.hwnd, cDC.GetSafeHdc(),0)
        bmInfo = dataBitMap.GetInfo()
        im = np.frombuffer(dataBitMap.GetBitmapBits(True),
                           dtype=np.uint8)  # Numpy Implementation
        #bmStr = dataBitMap.GetBitmapBits(True)
        #im = Image.frombuffer('RGB',(bmInfo['bmWidth'],bmInfo['bmHeight']),bmStr,'raw','BGRX',0,1) PIL is too slow
        dcObj.DeleteDC()
        cDC.DeleteDC()
        win32gui.ReleaseDC(self.hwnd, wDC)
        win32gui.DeleteObject(dataBitMap.GetHandle())
        #Bitmap has 4 channels like: BGRA. Discard Alpha and flip order to RGB
        if im.size < 4: return np.zeros((1, 1, 3), dtype=np.uint8)
        return np.array(
            im.reshape(bmInfo['bmHeight'], bmInfo['bmWidth'], 4)[:, :, -2::-1])