Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mss.exception.ScreenShotError: gdi32.GetDIBits() failed. #135

Closed
AlohaLon opened this issue Dec 24, 2019 · 2 comments
Closed

mss.exception.ScreenShotError: gdi32.GetDIBits() failed. #135

AlohaLon opened this issue Dec 24, 2019 · 2 comments

Comments

@AlohaLon
Copy link

General information:

  • OS name: Windows10
  • OS architecture: 64 bits
  • Resolutions:
    • Monitor 1: 1920x1080
    • Monitor 2: 1920x1080
  • Python version: 3.7.4
  • MSS version: 4.0.3

I am trying to capture specific parts of my screen so I can train an agent with reinforcement learning. For that reason I want high FPS screen input for really long periods of time (around 8 hours per session). So far I've been using really basic and simple code to test the mss python library, but after some minutes (it should be around 5) it drops an exception. Should be noted that the script works perfectly up to the point that throws the exception. Here's the code of the script:

import numpy as np
import cv2
from mss import mss

bounding_box_notes = {'top': 0, 'left': 310, 'width': 400, 'height': 200}
bounding_box_score = {'top': 0, 'left': 1440, 'width': 480, 'height': 80}
bounding_box_test = {'top': 350, 'left': 310, 'width': 400, 'height': 200}
with mss() as sct:
    while True:
        sct_img = sct.grab(bounding_box_notes)
        cv2.imshow('notes', np.array(sct_img))

        sct_img = sct.grab(bounding_box_test)
        cv2.imshow('test', np.array(sct_img))

        sct_img = sct.grab(bounding_box_score)
        cv2.imshow('score', np.array(sct_img))

        if (cv2.waitKey(1) & 0xFF) == ord('q'):
            cv2.destroyAllWindows()
            break

it basically creates 3 separate windows of specific parts of the screen and it works with the desired fps, but it crashes with the following exception message:

Traceback (most recent call last):
  File "C:/Users/Old_Friend/PycharmProjects/video_input/video_input.py", line 28, in <module>
    sct_img = sct.grab(bounding_box_score)
  File "C:\Users\Old_Friend\Anaconda3\lib\site-packages\mss\windows.py", line 301, in grab
    raise ScreenShotError("gdi32.GetDIBits() failed.")
mss.exception.ScreenShotError: gdi32.GetDIBits() failed.

it seems like people have already shared this error here (when using windows?), but it does not seem like it is fixed yet.
I also tried:

  • declaring sct inside of the while,
  • declaring sct inside of the while and .close() right before declaring it,
  • catching the exception and ignoring it through continue, but it continued throwing the same exception non stop after the initial exception

RAM or CPU usage are not a concern. They stay at low values as shown in task manager.

@BoboTiG
Copy link
Owner

BoboTiG commented Dec 24, 2019 via email

@AlohaLon
Copy link
Author

it works like a charm! amazing performance after 2 hours non stop, can't thank you enough!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants