从屏幕流式传输帧,生成视频

Streaming frames from the screen, generating a video

如果我想刷新图像,我必须关闭 window 才能刷新它。不一直关闭window可以吗? 这是代码:

import numpy
import cv2
from PIL import ImageGrab
while True:

    img = ImageGrab.grab(bbox=(765,155,1135,195))
    img_np = numpy.array(img)
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
    cv2.imshow("frame",frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

cv2.waitKey(0)等待无超时,替换为cv2.waitKey(time_in_msec)

以大约 10Hz 刷新的示例:

import numpy
import cv2
from PIL import ImageGrab

while True:
    img = ImageGrab.grab(bbox=(765,155,1135,195))
    img_np = numpy.array(img)
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
    cv2.imshow("frame",frame)
    cv2.waitKey(100)

cv2.destroyAllWindows()

参见 waitKey 注释:

imshow
Displays an image in the specified window.

Note This function should be followed by waitKey function which displays the image for specified milliseconds.
Otherwise, it won’t display the image.
For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display).
waitKey(25) will display a frame for 25 ms, after which display will be automatically closed.