我是新手。我尝试使用 mss 来截屏监视器。我的代码:

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

当我运行代码时,此显示错误:

  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>})

拜托,有人可以告诉我解决这个错误


编辑:这是由于资源未释放。它已在 MSS 4.0.0 或更新版本中修复。

您可以尝试在循环之外使用 MSSfor吗?就像是:

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)

错误消息不是很明确,但它告诉您无法截取屏幕截图(没有检索到位)。但我们无法知道原因。可能是内存错误。

另外,尝试打印i以查看它是否早期出现错误。


小提醒:stackoverflow.com/help/someone-answers,如果答案对你有好处的话:)