4

What is the most efficient way to capture screen in python using modules eg PIL or cv2? Because It takes up a lot of ram.

I wanted to teach AI to play dino game of Chrome through screen scraping and neat but it is way to slow...

I have tried:

import numpy as np
from PIL import ImageGrab
import cv2
import time

last_time = time.time()
while True:
    printscreen_pil = ImageGrab.grab(bbox= (0, 40, 800, 640))

    printscreen_numpy = np.array(printscreen_pil.getdata(), dtype = 'uint8').reshape((printscreen_pil.size[1], printscreen_pil.size[0], 3))
    
    print(f'the loop took {time.time() - last_time} seconds')
    last_time = time.time()
 
     cv2.imshow('window', printscreen_numpy)
     if cv2.waitKey(25) & 0xFF == ord('q'):
         cv2.destroyAllWindows()
         break

> 
    # average time = the loop took 2.068769931793213 seconds 
DuDa
  • 3,718
  • 4
  • 16
  • 36
SHRIDU MANISH
  • 59
  • 1
  • 6

1 Answers1

7

You can use mss which is an "An ultra fast cross-platform multiple screenshots module in pure python".

For example:

import time
import cv2
import numpy as np
from mss import mss

start_time = time.time()
mon = {'top': 200, 'left': 200, 'width': 200, 'height': 200}
with mss() as sct:
    while True:
        last_time = time.time()
        img = sct.grab(mon)
        print('The loop took: {0}'.format(time.time()-last_time))
        cv2.imshow('test', np.array(img))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

Result:

The loop took: 0.024120092391967773

Output:

enter image description here

The result is faster 100x than your current result.

Ahmet
  • 7,527
  • 3
  • 23
  • 47
  • line 11, in img = sct.grab(mon) AttributeError: 'MSS' object has no attribute 'grab' – SHRIDU MANISH Sep 03 '20 at 15:00
  • 1
    Did you install `pip install mss`? Do you have any other files named as `mss.py`? – Ahmet Sep 03 '20 at 15:12
  • 1
    Also which python version are you using? I installed on `python3.7` – Ahmet Sep 03 '20 at 15:14
  • 1
    Wait! did you use `import mms` or `from mms import mms`? which one? – Ahmet Sep 03 '20 at 15:18
  • From mms import mms. I think that is the problem but I just copied your code and tested so its exactly the same. And yes I did pip install mms. So should i only use import mms? – SHRIDU MANISH Sep 03 '20 at 18:47
  • I'm trying to understand where could be the error. If you copy and paste the code it should work, since I got the result. No `from mms import mms` is the correct usage. – Ahmet Sep 03 '20 at 18:50
  • Why do you use `sct = mms()`? – Ahmet Sep 08 '20 at 08:22
  • well now it works likes charm. Dont know what changed and yes the output was great though not as fast as yours: The loop took: 0.03593921661376953 – SHRIDU MANISH Sep 08 '20 at 08:27
  • 1
    Glad, if I could help and congrats on solving the problem :) – Ahmet Sep 08 '20 at 08:29
  • One of the comments was a question "Why do you use sct = mms()". I tested and the reason is to speed things up, calling mms() once and later reuse it as sct, or calling mms() every time - difference is by up to an order of magnitude in speed, and obviously calling mms() only once is faster. – Lissanro Rayen Jun 19 '22 at 01:14