You will need to use ImageGrab from the Pillow library (PIL) and convert the capture to a numpy array. When you have an array, you can do what you like using opencv. I converted the capture to gray and used imshow () as a demo.
Here is a quick code to get you started:
from PIL import ImageGrab import numpy as np import cv2 img = ImageGrab.grab(bbox=(100,10,400,780))
you can connect the array there with the frequency you want to save frames. After that, you simply decode the frames. don't forget to add before the loop:
fourcc = cv2.VideoWriter_fourcc(*'XVID') vid = cv2.VideoWriter('output.avi', fourcc, 6, (640,480))
and inside the loop you can add:
vid.write(frame)
UPDATE
the end result looks something like this (if you want to achieve the stream of frames that you have. Storing as a video is just a demonstration of the use of opencv on the screen):
from PIL import ImageGrab import numpy as np import cv2 while(True): img = ImageGrab.grab(bbox=(100,10,400,780))
Hope that helps
source share