是否有任何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 + 1else: 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)
2 回答
浮云间
TA贡献1829条经验 获得超4个赞
您可以通过以下方式抓取屏幕pyautogui:
import pyautogui
image = pyautogui.screenshot('filename.png')
慕桂英4014372
TA贡献1871条经验 获得超13个赞
您可以这样做:)我认为Mac OS无法使用这些WiIn32gui库。相反,您可以使用枕头抓屏。屏幕大小可以更改,具体取决于所需的大小。
import cv2
import numpy as np
import pyautogui
from PIL import ImageGrab
screen_w = 1920
screen_h = 1080
while 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
- 2 回答
- 0 关注
- 839 浏览
添加回答
举报
0/150
提交
取消