Is there a way with Python (maybe with OpenCV or PIL) to continuously grab frames of all or a portion of the screen, at least at 15 fps or more? I've seen it done in other languages, so in theory it should be possible.
I do not need to save the image data to a file. I actually just want it to output an array containing the raw RGB data (like in a numpy array or something) since I'm going to just take it and send it to a large LED display (probably after re-sizing it).
You can try this=>
based on this post and others posts, i made something like this . Its taking a screenshot and writing into a video file without saving the img.
With all of the above solutions, I was unable to get a usable frame rate until I modified my code in the following way:
With this solution, I easily get 20+ frames/second.
For reference, check this link: https://python-mss.readthedocs.io/examples.html
You will need to use ImageGrab from Pillow (PIL) Library and convert the capture to numpy array. When you have the array you can do what you please with it using opencv. I converted capture to gray color and used imshow() as a demonstration.
Here is a quick code to get you started:
you can plug an array there with the frequency you please to keep capturing frames. After that you just decode the frames. don't forget to add before the loop:
and inside the loop you can add:
UPDATE
the end result look something like this (If you want to achieve a stream of frames that is. Storing as video just a demonstration of using opencv on the screen captured):
Hope that helps
I've tried
ImageGrab
fromPIL
and it gave me 20fps which is ok but using win32 libraries gave me +40fps which is amazing!I used this code by Frannecklp but it didn't work just fine so I needed to modify it:
-Firstly
pip install pywin32
in case using the libraries-import the libraries like this instead:
for geting a simple image screen do:
and for getting frames:
I tried all of the above but it did not give me the real-time screen update. You can try this. This code is tested and worked successfully and also give you a good fps output. You can also judge this by each loop time it's needed.