ImageGrab.grab() method is too slow

前端 未结 3 514
天涯浪人
天涯浪人 2021-01-06 01:44

So i need to get a bunch of screenshots every second, like 5. I am using it to program a bot for a game. However imagegrab method takes like 0.3 seconds, which is terribly s

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 02:26

    Another solution is to use Python MSS.

    from mss import mss
    from PIL import Image
    
    def capture_screenshot():
        # Capture entire screen
        with mss() as sct:
            monitor = sct.monitors[1]
            sct_img = sct.grab(monitor)
            # Convert to PIL/Pillow Image
            return Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX')
    
    img = capture_screenshot()
    img.show()
    

    This function can return screenshots at up to 27 fps on my slow laptop.

提交回复
热议问题