I’m in the process of building an automated game bot in Python on OS X 10.8.2 and in the process of researching Python GUI automation I discovered autopy. The mouse manipulation API is great, but it seems that the screen capture methods rely on deprecated OpenGL methods…
Are there any efficient ways of getting the color value of a pixel in OS X? The only way I can think of now is to use os.system("screencapture foo.png") but the process seems to have unneeded overhead as I’ll be polling very quickly.
A small improvement, but using the TIFF compression option for
screencaptureis a bit quicker:This does have a lot of overhead, as you say (the subprocess creation, writing/reading from disc, compressing/decompressing).
Instead, you could use PyObjC to capture the screen using
CGWindowListCreateImage. I found it took about 70ms (~14fps) to capture a 1680×1050 pixel screen, and have the values accessible in memoryA few random notes:
Quartz.CoreGraphicsmodule is the slowest part, about 1 second. Same is true for importing most of the PyObjC modules. Unlikely to matter in this case, but for short-lived processes you might be better writing the tool in ObjCCGDataProviderCopyDatacall – I wonder if there’s a way to access the data directly, since we dont need to modify it?ScreenPixel.pixelfunction is pretty quick, but accessing large numbers of pixels is still slow (since0.01ms * 1650*1050is about 17 seconds) – if you need to access lots of pixels, probably quicker tostruct.unpack_fromthem all in one go.Here’s the code: