python mss mss.exception.ScreenShotError:

3k Views Asked by At

I'm newbie. I try use mss to screenshot monitor. My code:

for i in range(1, 20000):
  cactus_box = {'left': 508, 'top': 382, 'width': 30, 'height': 33}
  sct = mss()
  sct_img = sct.grab(cactus_box)

when i run code, this display error:

  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mss\windows.py", line 203, in grab
raise ScreenShotError('gdi32.GetDIBits() failed.', locals())
mss.exception.ScreenShotError: ('gdi32.GetDIBits() failed.', {'bits': 0, 'height': 33, 'width': 30, 'gdi': <WinDLL 'gdi32', handle 75f00000 at 0x93dd5f0>, 'monitor': {'left': 508, 'top': 382, 'width': 30, 'height': 33}, 'self': <mss.windows.MSS object at 0x093DD810>})

Please, someone can show me solve this error

1

There are 1 best solutions below

1
On

EDIT: this is due to resources not freed. It is fixed in MSS 4.0.0 or newer.

Could you try to use MSS ouside the for loop? Something like:

with mss() as sct:
    cactus_box = {'left': 508, 'top': 382, 'width': 30, 'height': 33}
    for i in range(1, 20000):
        sct_img = sct.grab(cactus_box)

The error message is not very explicit but it tells you that the screenshot is not possible (no bits where retrieved). But we cannot know the cause. A memory error perhaps.

Also, try to print i to see if it bugs early.