如何使用p.locateCenterOnScreen一次查找两张图像



所以我有一些代码在循环中运行,试图找到Microsoft Edge。一旦找到它,它将打破循环,继续执行程序的其余部分。这是代码:

#! /usr/bin/env python
import pyautogui as p
import time
import random
from hashlib import sha256
def search(query):
p.hotkey("ctrl","e")
time.sleep(.1)
p.write(query)
p.press("enter")
time.sleep(.67)
def imageFind(image,g,double):
a = 0
b = 0
while(a==0 and b==0):
try:
a,b = p.locateCenterOnScreen(image,grayscale=g)
except TypeError:
pass
if double == True:
time.sleep(1)
p.doubleClick(a,b)
else:
p.click(a,b)
return a,b
#p.hotkey("win","d")
counter = 1
while counter == 1:
image_find = imageFind("Edge.png",False,False)
if all(image_find):
counter = 0
p.write(["win"])
time.sleep(1)
p.write("notepad")
time.sleep(1)
p.write(["enter"])
time.sleep(1)
p.write("Found Edge")

如何同时对两个图像执行此操作?例如,我尝试过:

counter = 1
while counter == 1:
image_find = imageFind("Edge.png",False,False)
image_find2 = imageFind("Firefox.png",False,False)
if all(image_find):
counter = 0
p.write(["win"])
time.sleep(1)
p.write("notepad")
time.sleep(1)
p.write(["enter"])
time.sleep(1)
p.write("Found Edge")
elif all(image_find2):
counter = 0
p.write(["win"])
time.sleep(1)
p.write("notepad")
time.sleep(1)
p.write(["enter"])
time.sleep(1)
p.write("Found Firefox")

但它似乎一直停留在image_find = imageFind("Edge.png",False,False)上,直到找到Edge.png,然后再转到它下面的下一行Firefox.png,这意味着必须首先找到Edge。

我如何构建它,使python同时寻找这两者,并遵循最先找到的条件?

谢谢!

注意:我刚刚意识到我甚至不需要while循环,因为除非找到图像,否则它无论如何都不会继续到下一行代码。

实现这一点的精炼代码:

counter = 1
while counter == 1:
Img_1 = p.locateCenterOnScreen(“img_1.png”)
if img_1 is not None:
counter = 0
Img_2 = p.locateOnCenterScreen(“img_2.png”)
if img_2 is not None:
counter = 0

删除:

def imageFind(image,g,double):
a = 0
b = 0
while(a==0 and b==0):
try:
a,b = p.locateCenterOnScreen(image,grayscale=g)
except TypeError:
pass

最新更新