Quickly getting the color of some pixels on the screen in Python on Windows 7

前端 未结 4 934
说谎
说谎 2020-12-04 22:44

I need to get the color of some pixels on the screen or from the active window, and I need to do so quickly. I\'ve tried using win32gui and ctypes/

4条回答
  •  醉话见心
    2020-12-04 23:33

    Disabling Windows Desktop Composition speeds pixel up reading A LOT.

    Computer -> Properties -> Advanced system settings -> Performance -> desktop composition [ ] (warning this disables Windows's transparency effects)

    Python 2.7 (Should be same for 3.x)

    win32gui.GetPixel()     #1.75s => 20ms
    windll.gdi32.GetPixel() #1.75s => 3ms (fastest)
    image.getpixel()        # 0.1s => 50ms
    px[]                    # 0.1s => 50ms
    

    AutoIt for comparison

    $timer = TimerInit()
    
    For $x = 0 To 100 Step 10
        For $y = 0 To 100 Step 10
            PixelGetColor($x,$y) ;slow => 1ms
        Next
    Next
    
    ConsoleWrite("Time: " & TimerDiff($timer)/1000 & @CRLF)
    

提交回复
热议问题