简体   繁体   English

捕获游戏像素时,Python PIL-ImageGrab不准确

[英]Python PIL-ImageGrab inaccurate when capturing game pixels

I am trying to capture the pixels of a game to script a bot. 我正在尝试捕获游戏像素来编写机器人脚本。 I have a simple function: 我有一个简单的功能:

def printPixel():
  while True:
    flags, hcursor, (x,y) = win32gui.GetCursorInfo()
    print x,y,':',ImageGrab.grab().getpixel((x,y))

This prints the current x,y coords and the RGB value of that pixel. 这将打印当前的x,y坐标和该像素的RGB值。 This works as expected on my desktop hovering over various icons and such, but the same function does not work in-game. 这可以在我的桌面上按预期工作,将鼠标悬停在各种图标等上,但是同一功能在游戏中不起作用。 Any thoughts? 有什么想法吗?

edit: When I save the image to a file and perform this same operation on the saved image, it works perfectly in-game. 编辑:当我将图像保存到文件并在保存的图像上执行相同的操作时,它在游戏中完美运行。 However, it is way slower. 但是,它慢得多。 I'd like to operate on the image in memory, and not from a file. 我想对内存中的图像进行操作,而不是对文件进行操作。

Video games often deal with th graphical system directly for performance reasons, so some of the typical windows apis might not work on them. 由于性能原因,视频游戏通常直接处理图形系统,因此某些典型的Windows API可能无法在其上运行。 Try and take a screenshot by pressing the print screen button. 尝试按屏幕打印按钮以截取屏幕截图。 If that captures your screen than you can take a screenshot in python and check the image you have captured taking into account the cursor position. 如果可以捕获屏幕,则可以使用python截屏并考虑光标位置来检查捕获的图像。

To take a screenshot on windows you can check out this answer to the question Fastest way to take a screenshot with python on windows it uses the win32gui library as you are using. 要在Windows上截屏,您可以查看以下问题的答案: 在Windows上使用python截屏的最快方法,它使用的是win32gui库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM