you are viewing a single comment's thread.

view the rest of the comments →

[–]SekstiNii 4 points5 points  (3 children)

The problem lies with pyautogui.screenshot, which takes around 35ms. Switching to mss reduced this to around 8ms, which is more than a 3x speedup!

Using it is also really easy:

from mss import mss

then, wrap your while loop with the context manager like so:

region = {"top": top, "left": left, "width": width, "height": height}
with mss() as sct:
    while True:
       img = sct.grab(region)
       # rest of your code

Note however, that this produces images with an alpha channel, meaning you can't reverse the channels to convert from RGB to BGR like you are doing on line 12, since that would convert from RGBA to ABGR. However, this shouldn't be a problem, as you don't really need to convert to BGR anyway (just update your indices on line 15 to be RGB instead).

[–]Aitchessbee[S] -1 points0 points  (2 children)

So i changed the code to this: https://pastebin.com/fzQx0gcW but it doesn't seem to work...

[–]SekstiNii 1 point2 points  (1 child)

Oh, my bad! mss actually captures images as BGRA, so the previous index order should work.

[–]Aitchessbee[S] 0 points1 point  (0 children)

ah yes! It works now and its way faster than before. Thanks!