我正在使用python中的mss模块对整个屏幕进行截图。我把屏幕分割成不同的区块,对这些特定的区块进行截图。在一个循环中这样做需要很长时间,而且时间会随着块的增加而增加。
这些是5块1920x1080的屏幕。
[{'top': 0, 'left': 0, 'width': 1920, 'height': 216}, {'top': 216, 'left': 0, 'width': 1920, 'height': 216}, {'top': 432, 'left': 0, 'width': 1920, 'height': 216}, {'top': 648, 'left': 0, 'width': 1920, 'height': 216}, {'top': 864, 'left': 0, 'width': 1920, 'height': 216}]
我使用多线程来做,但它产生的图像很模糊,而且只对一些区块进行截图,而不是全部(比如有些会很清晰,有些会很黑)。
我正在无限循环地做所有这些事情,并将图像发送到服务器。
def main(block):
image = sct.grab(block).rgb
# send image to server
with mss.mss() as sct:
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(main, blocks)
上面的代码产生了不好的图像(图像的一些区块只是黑色),所以我试着这样做。
def threaded(func, args):
threads = []
for arg in args:
thread = threading.Thread(target=func, args=(arg[0],arg[1],))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
def main(block, sct):
with threading.Lock():
image = sct.grab(block).rgb
image = sct.grab(block).rgb
# send image to server
with mss.mss() as sct:
threaded(main, zip(blocks, [sct for i in range(len(blocks))]))
上述代码给出了这个错误。
Exception in thread Thread-165:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "client.py", line 31, in main
image = sct.grab(block).rgb
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mss\windows.py", line 301, in grab
raise ScreenShotError("gdi32.GetDIBits() failed.")
mss.exception.ScreenShotError: gdi32.GetDIBits() failed.