OpenCV录屏显示
import cv2
import numpy as np
import win32api
import win32gui
import win32ui
import win32con
width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
print(width, height)
hDC = win32gui.GetDC(0)
dcObj = win32ui.CreateDCFromHandle(hDC)
memDC = dcObj.CreateCompatibleDC()
screenshot = win32ui.CreateBitmap()
screenshot.CreateCompatibleBitmap(dcObj, width, height)
memDC.SelectObject(screenshot)
while True:
memDC.BitBlt((0, 0), (width, height), dcObj, (0, 0), win32con.SRCCOPY)
img = np.fromstring(screenshot.GetBitmapBits(True), dtype='uint8')
img.shape = (height, width, 4)
print(img)
cv2.imshow('Screen', img)
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()
memDC.DeleteDC()
win32gui.ReleaseDC(win32con.NULL, hDC)
效果