您将需要使用 Pillow (PIL) 库中的 ImageGrab 并将捕获转换为 numpy 数组。当您拥有数组时,您可以使用 opencv 随心所欲地使用它。我将捕获转换为灰色并使用 imshow() 作为演示。
这是一个快速入门的代码:
from PIL import ImageGrab
import numpy as np
import cv2
img = ImageGrab.grab(bbox=(100,10,400,780)) #bbox specifies specific region (bbox= x,y,width,height *starts top-left)
img_np = np.array(img) #this is the array obtained from conversion
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
cv2.imshow("test", frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
您可以按照您喜欢的频率插入一个阵列以继续捕获帧。之后,您只需解码帧。不要忘记在循环之前添加:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vid = cv2.VideoWriter('output.avi', fourcc, 6, (640,480))
您可以在循环内添加:
vid.write(frame) #the edited frame or the original img_np as you please
更新
最终结果看起来像这样(如果你想实现帧流。存储为视频只是在捕获的屏幕上使用 opencv 的演示):
from PIL import ImageGrab
import numpy as np
import cv2
while(True):
img = ImageGrab.grab(bbox=(100,10,400,780)) #bbox specifies specific region (bbox= x,y,width,height)
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
cv2.imshow("test", frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
希望有帮助