Currently I’m using a pixel reader via AutoItv3 to perform some actions in a program that is running direct X; A game. Right now the program works fine but as an exercise I’ve been rewriting it in python. Right now I can do:
import ImageGrab # Part of PIL
image = ImageGrab.grab() #Define an area to capture.
rgb = image.getpixel((1, 90)) #What pixel do we want?
And that grabs the pixel info I want just fine, but I’m doing this quite rapidly (needs to be done 3x a second or faster), but the result is that it majorly affects the framerate of this DirectX-based game.
Is there a faster way in Python to read a specific screen pixel? Even limiting this one to running every 0.3 seconds is causing more strain than it really should (I actually figured python would be faster than AutoIt for this particular purpose, hence the reason I’m trying it)
This is the PIL’s grabscreen source, Its does not accept any parameters, and Its grab the whole screen and convert it to bitmap.
So, when I just go a little deep, found C approach is good
http://msdn.microsoft.com/en-us/library/dd144909(VS.85).aspx
And Python has ctypes, so here is my approach using ctypes (in Windows 10,
winnthas been replaced withWindows):You could write a wrapper for function GetPixel like this
Then you can use like
getpixel(0,0),getpixel(100,0), etc…PS: Mine is Windows 2000, so I putwinntin the path, you may need to change it towindowsor you chould completely remove path, just usinguser32.dllandgdi32.dllshould work too.