Python: 有没有办法用cv2灰度屏?

Python: Is there a way to grayscale screen with cv2?

是否可以使用 cv2 对屏幕的一部分进行实时灰度化?

我知道如何灰度化图像,但我们可以对屏幕进行灰度化吗? 如果您知道如何对屏幕的一部分进行灰度缩放,请添加代码!

cv2 没有对您的屏幕缓冲区的写入权限,因此无法更改您在屏幕上看到的颜色。但是,您应该能够截取屏幕截图并对其进行操作。参见 Python take a screenshot of the screen and save it to a buffer

我找到了 gray-scale 部分屏幕的方法!

我的代码:

from PIL import ImageGrab
import cv2
from numpy import array
import time

while True:
    Screen = array(ImageGrab.grab(bbox=(13, 32,805, 623)))
    gray = cv2.cvtColor(Screen, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 20, 30)
    edges_high_thresh = cv2.Canny(gray, 60, 120)

    
    k = cv2.waitKey(30) & 0xff

    if k == 27:
        cv2.destroyAllWindows()
        break
    
    
    cv2.imshow('Frame', edges)