def screenshot(filename, hwnd=None): """ Take the pyqt_screenshot of Windows app Args: filename: file name where to store the pyqt_screenshot hwnd: Returns: bitmap pyqt_screenshot file """ # import ctypes # user32 = ctypes.windll.user32 # user32.SetProcessDPIAware() if hwnd is None: """all screens""" hwnd = win32gui.GetDesktopWindow() # get complete virtual screen including all monitors w = win32api.GetSystemMetrics(SM_CXVIRTUALSCREEN) h = win32api.GetSystemMetrics(SM_CYVIRTUALSCREEN) x = win32api.GetSystemMetrics(SM_XVIRTUALSCREEN) y = win32api.GetSystemMetrics(SM_YVIRTUALSCREEN) else: """window""" rect = win32gui.GetWindowRect(hwnd) w = abs(rect[2] - rect[0]) h = abs(rect[3] - rect[1]) x, y = 0, 0 hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0, 0), (w, h), mfcDC, (x, y), win32con.SRCCOPY) # saveBitMap.SaveBitmapFile(saveDC, filename) bmpinfo = saveBitMap.GetInfo() bmpstr = saveBitMap.GetBitmapBits(True) pil_image = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1) cv2_image = pil_2_cv2(pil_image) mfcDC.DeleteDC() saveDC.DeleteDC() win32gui.ReleaseDC(hwnd, hwndDC) win32gui.DeleteObject(saveBitMap.GetHandle()) # cv2.imshow('toolIcon',cv2_image) # cv2.waitKey(0) return cv2_image
def snapshot(self, filename="tmp.png"): """ Take a screenshot and save it to `tmp.png` filename by default Args: filename: name of file where to store the screenshot Returns: display the screenshot """ w, h = self.get_current_resolution() dsp = display.Display() root = dsp.screen().root raw = root.get_image(0, 0, w, h, X.ZPixmap, 0xffffffff) image = Image.frombytes("RGB", (w, h), raw.data, "raw", "BGRX") from airtest.aircv.utils import pil_2_cv2 image = pil_2_cv2(image) return image