python实时获取屏幕

在 Python 中实时获取屏幕截图的方法有很多,其中一种方法是使用 mss 库。mss 是一个用 Python 编写的跨平台库,它可以轻松地截取屏幕。

安装 mss:

pip install mss

示例代码:

import mss
import cv2
with mss.mss() as sct:
    # Get the screen shot of the 1st monitor
    sct_img = sct.grab(sct.monitors[0])
    # Convert to a OpenCV image
    img = np.array(sct_img)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # Display the image
    cv2.imshow("Screenshot", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

这段代码将截取第一个显示器的屏幕截图,然后使用 OpenCV 将其显示出来。

  •