【发布时间】:2021-03-27 00:42:10
【问题描述】:
我的程序目前存在问题,我不太确定如何解决。
我正在做以下事情:
x = 0
y = 0
im = ImageGrab.grab()
time.sleep(1)
while True:
xy = (x, y)
x = x + 1
if im.getpixel(xy) == (0,158,187):
time.sleep(0.3)
pyautogui.click(x,y)
break
if x >= 1200:
x = 0
y = y + 1
print('cant find pixel')
if y >= 950:
y = 0
x = 0
它在大约 90% 的时间里工作,然后有这个随机时间它只是说它无法检测到像素,尽管像素在那里 100%。
编辑:设法在它发生的 10% 中捕获以下错误:
AttributeError: 'NoneType' object has no attribute 'getpixel'
这没有任何意义,因为我事先在做 im = ImageGrab.grab() 并且它在 90% 的时间里都有效
【问题讨论】:
-
问题不在于它无法检测到像素。问题是
ImageGrab.grab失败了。您可能需要检查并稍后重试。为什么抓屏后要等一秒再搜索位图? -
@TimRoberts 没有具体原因,问题是如果需要,该程序将不间断运行 8 小时,我不能让 ImageGrab.grab 无缘无故随机失败。我通过这样做找到了一个半修复: while True: if im is None: im = ImageGrab.grab() else: break 问题是它有时仍然会以某种方式发生......
标签: python python-3.x python-imaging-library cv2