猿问

grabscreen.py Python win32api

是否有任何Win32gui或此代码的Linux或Mac OS等效库?在外部项目上工作,此Windows代码将帮助我抓取屏幕。Havent能够找到任何相似的库。谢谢


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)


喵喵时光机
浏览 840回答 2
2回答

浮云间

您可以通过以下方式抓取屏幕pyautogui:import pyautoguiimage = pyautogui.screenshot('filename.png')

慕桂英4014372

您可以这样做:)我认为Mac OS无法使用这些WiIn32gui库。相反,您可以使用枕头抓屏。屏幕大小可以更改,具体取决于所需的大小。import cv2import numpy as npimport pyautoguifrom PIL import ImageGrabscreen_w = 1920screen_h = 1080while True:    rgb = ImageGrab.grab(bbox=(0, 0, screen_w, screen_h))     rgb = np.array(rgb)    cv2.imshow('window_frame', rgb)    if cv2.waitKey(1) & 0xFF == ord('q'):        break
随时随地看视频慕课网APP
我要回答