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')
Пример #2
0
def track_window_position(monitor_dict):
    hwnd = monitor_dict["hwnd"]
    rw = win32gui.GetWindowRect(hwnd)
    rc = win32gui.GetClientRect(hwnd)
    window_width = rw[2] - rw[0]
    border_width = window_width - rc[
        2]  # the client's right side is before the end of the window
    border_width -= 9  # ???
    window_height = rw[3] - rw[1]
    border_height = window_height - rc[3]
    border_height -= 8  # ?
    # border_height =
    monitor_dict["left"] = rw[0] + border_width
    monitor_dict["top"] = rw[1] + border_height + SM_HUD_HEIGHT
    # monitor_dict["width"] = rc[2] + 1
    monitor_dict["width"] = SM_SIZE[0]
    # monitor_dict["height"] = rc[3] - SM_HUD_HEIGHT
    monitor_dict["height"] = SM_SIZE[1] - SM_HUD_HEIGHT - CROP_BOTTOM
Пример #3
0
def screencap(window_title):
    if window_title:
        hwnd = win32gui.FindWindow(None, window_title)
        if hwnd:
            win32gui.SetForegroundWindow(hwnd)
            x, y, x1, y1 = win32gui.GetClientRect(hwnd)  #get client size
            x, y = win32gui.ClientToScreen(hwnd, (x, y))
            x += 200
            y += 50  #cropping out top part
            x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y))
            ##            print(x, y, x1, y1)
            x1 -= 400  #cropping out right side
            y1 -= 580  #cropping out bottom part
            im = pyautogui.screenshot(region=(x, y, x1, y1))
            im.save(r'D:\Studies\IDP\SCREENSHOT.png')
        else:
            print('Window not found!')
    else:
        im = pyautogui.screenshot()
        return im
Пример #4
0
    def screenshot_by_pid(self, procid, maximized=False):
        """
        Method to take the screenshot by PID, this makes the magic works
        """
        self.__HWND = None

        def getHwnd(hwnd, procid):
            if procid in win32process.GetWindowThreadProcessId(hwnd):
                if win32gui.IsWindowVisible(hwnd):
                    self.__HWND = hwnd
        win32gui.EnumWindows(getHwnd, procid)
        if self.__HWND != None:
            win32gui.SetForegroundWindow(self.__HWND)
            x, y, x1, y1 = win32gui.GetClientRect(self.__HWND)
            if maximized == False:
                # Fix only for not maximized window
                x, y = win32gui.ClientToScreen(self.__HWND, (x, y))
            x1, y1 = win32gui.ClientToScreen(self.__HWND, (x1 - x, y1 - y))
            im = pyautogui.screenshot(region=(x, y, x1, y1))
            return im
        else:
            printc("Error taking screenshot", "error")
    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()
Пример #6
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])
Пример #7
0

def imageThere(images):
    img = d.get_latest_frame()
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    for i in images:
        res = cv2.matchTemplate(img_gray, i[0], cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        if max_val >= 0.8:
            return i[1]
    return None

HWND = win32gui.FindWindow(None, 'Fable 3')
win32gui.SetForegroundWindow(HWND)

x, y, x1, y1 = win32gui.GetClientRect(HWND)
x, y = win32gui.ClientToScreen(HWND, (x, y))
x1, y1 = win32gui.ClientToScreen(HWND, (x1 - x, y1 - y))
RAW_REGION = (x, y, x1, y1)
REGION = (x, int(y1*0.7), x1, y1)

print("""
        ----------------------------------------------------
        beep beep... showing image of what will be searched.
            """)

window = Tk()
window.overrideredirect(True)
window.wm_attributes("-topmost", True)
window.wm_attributes("-transparentcolor", "white")
window.attributes("-alpha", 0.3)
Пример #8
0
 def __get_client_rect(self):
     # https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclientrect
     return win32gui.GetClientRect(self.__hwnd)