本文介绍了PyAutoGui.pixelMatchesColor() 返回不可能的值,使用限制为 10,000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序来在 this 站点上绘制图片,使用 PyAutoGui 移动鼠标,单击,并检查屏幕上的一些像素颜色.您可以在画布的左上角看到我的测试.我的程序严重依赖 pyautogui.pixelMatchesColor() 函数,在某些时候我的程序似乎崩溃了.在试图找到会导致同样问题的最小代码集后,我得到了这个:

导入pyautogui否 = 0为真:如果 pyautogui.pixelMatchesColor(1750, 180, (255, 255, -1)):休息数量 += 1打印(数量)

这个,你可能已经猜到了:

10000

这里的问题是,一旦你在一次运行中使用该函数超过 10,000 次,它就会停止工作并且只返回:

(255, 255, -1)

我环顾四周,但在 pyautogui.pixelMatchesColor() 函数的使用限制为 10,000 的任何地方都找不到任何东西(顺便说一句,此限制也适用于 pyautogui.pixel() 函数).它破坏了我的程序,所以如果你有任何信息,或者有办法绕过这个问题,请告诉我.谢谢!

在查看 pyautogui 代码后,发现它使用 ctypes 进行鼠标控件和 PIL 用于屏幕实用程序.我将尝试使用它们而不是 pyautogui 来获得更直接的代码,看看它是否有所作为.

解决方案

这可能有助于深入了解此错误.我跑了,pyautogui.pixel() 直到它坏了.然后我尝试了 viddle 建议的解决方法...

from PIL import ImageGrabpixelRGB = ImageGrab.grab().getpixel((100, 125))

引发以下异常

回溯(最近一次调用最后一次):文件<pyshell#16>",第 1 行,在 <module> 中ImageGrab.grab().getpixel((140,20))

回溯(最近一次调用最后一次):文件",第 1 行,在ImageGrab.grab().getpixel((140,20))文件C:\Users\XisUnknown\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\ImageGrab.py",第 41 行,抓取大小,数据 = 抓取器()OSError:屏幕抓取失败

I am creating a program to draw pictures on this site, using PyAutoGui to move the mouse, click, and check some pixel colours on the screen. You can see my testing in the top left hand corner of the canvas. My program relies heavily on the pyautogui.pixelMatchesColor() function, and at certain points my program seemed to break. After trying to find the smallest set of code which would result in the same problem, I got to this:

import pyautogui
no = 0
while True:
    if pyautogui.pixelMatchesColor(1750, 180, (255, 255, -1)):
        break
    num += 1
print(num)

This, as you may have guessed outputted:

10000

The problem here is that once you have used the function more than 10,000 times in one run, it stop working and only returns:

(255, 255, -1)

I have looked around, but can't find anything anywhere on a usage limit of 10,000 for the pyautogui.pixelMatchesColor() function (btw this limit also applies to the pyautogui.pixel() function). It has broken my program, so if you have any information, or a way to circumvent this issue then please let me know. Thank you!

EDIT: After looking into the pyautogui code, it turns out it uses ctypes for mouse controls and PIL for screen utilities. I will try using them instead of pyautogui for more direct code to see if it makes a difference.

解决方案

This might provide insight into this bug. I ran, pyautogui.pixel() until it breaks. I then tried the workaround suggested by viddle...

from PIL import ImageGrab

pixelRGB = ImageGrab.grab().getpixel((100, 125))

Which raised the following exception

Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
ImageGrab.grab().getpixel((140,20))

这篇关于PyAutoGui.pixelMatchesColor() 返回不可能的值,使用限制为 10,000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:37