python中pyautogui.locateOnScreen如何识别多张图片

import pyautogui

Royal_image = pyautogui.locateCenterOnScreen('Royal.png', grayscale=True, confidence=0.5, region=(50, 55, 310, 230))
Royal_image1 = pyautogui.locateCenterOnScreen('Royal1.png', grayscale=True, confidence=0.5, region=(50, 55, 310, 230))

print('我想要的效果')
        pyautogui.locateCenterOnScreen('Royal.png'|'Royal2.png'|'Royal3.png', grayscale=True, confidence=0.5, region=(50, 55, 310, 230))

用于识别图片,图片具有一定变化.

img

img

img

img


x张图片都识别一次,如果有一张是xy,返回xy,如果全是None,返回None.
不是循环寻找.

可以借鉴下

import pyautogui
import time


def zan():
    time.sleep(0.5)   
    xy = pyautogui.locateOnScreen('dianzan.png')  # 寻找刚才保存点赞手势图片
    center = pyautogui.center(xy) # 寻找图片的中心 
    pyautogui.click(center)   
    print('点赞成功!')

count=10#为了防止一直运行下去,点一点数量的赞就结束,自己定义
while True:
    if pyautogui.locateOnScreen('dianzan.png'):
        zan()   # 调用点赞函数
        count-=1
    else:
        pyautogui.scroll(-500)    #本页没有图片后,滚动鼠标;
        print('没有找到目标,屏幕下滚~')
print("点10个赞结束!")


要识别多张图片,只能用循环操作

可以借鉴下

import pyautogui
import time


def zan():
    time.sleep(0.5)   
    xy = pyautogui.locateOnScreen('dianzan.png')  # 寻找刚才保存点赞手势图片
    center = pyautogui.center(xy) # 寻找图片的中心 
    pyautogui.click(center)   
    print('点赞成功!')

count=10#为了防止一直运行下去,点一点数量的赞就结束,自己定义
while True:
    if pyautogui.locateOnScreen('dianzan.png'):
        zan()   # 调用点赞函数
        count-=1
    else:
        pyautogui.scroll(-500)    #本页没有图片后,滚动鼠标;
        print('没有找到目标,屏幕下滚~')
print("点10个赞结束!")


以下答案由GPT-4.5Plus大模型(语音)与博主A努力中.......共同编写:
在Python中,可以使用pyautogui.locateOnScreen()方法识别屏幕上的图像。但是,如果要识别多张图片,需要用一些技巧。

一些常用的方法有:

  1. 每张图片单独识别,取最佳匹配结果
    可以分别调用locateOnScreen()方法识别每张图片,然后从识别结果中选择匹配度最高的一个,如:
python
loc1 = pyautogui.locateOnScreen('img1.png')
loc2 = pyautogui.locateOnScreen('img2.png')
loc3 = pyautogui.locateOnScreen('img3.png')
best_loc = loc1 or loc2 or loc3  # 取最佳匹配结果

  1. 将图片拼接到一起,识别拼接后的大图
    可以使用Image.open()方法打开多张图片,并使用Image.resize()调整到同一大小,然后使用Image.vstack()或Image.hstack()拼接到一起,最后识别拼接后的大图。如:
python
img1 = Image.open('img1.png')
img2 = Image.open('img2.png')
img3 = Image.open('img3.png')
img1.resize((100, 100)) 
img2.resize((100, 100))
img3.resize((100, 100))
 combined_img = Image.vstack((img1, img2, img3))
loc = pyautogui.locateOnScreen(combined_img)

  1. 设置识别参数提高容忍度
    可以调整pyautogui.locateOnScreen()的参数来提高容忍度,如:
  • grayscale=True:使图片转为灰度,提高匹配灵活度
  • threshold=0.8:设置匹配度阈值,降低要求以识别更模糊的图片

设置这些参数可以在一定程度上识别多张相似但不完全一致的图片。

提供一个参考思路:使用一个列表来存储要查找的图片
如果你要在多张图片中查找特定的图片,你可以将图片路径存储在一个列表中,然后在循环中使用该列表。
【首先定义要查找的图片路径列表,然后在循环中,使用 pyautogui.locateOnScreen() 函数查找每张图片,并将其位置存储在 location 变量中。如果找到了图像,则输出图像的位置;否则,输出找不到图像的提示。这个示例代码仅能够找到第一次找到的图片,如果找不到任何图片,则 location 变量为 None。】
参考示例代码:

import pyautogui  
import time  
  
# 定义要查找的图片路径列表  
images = ['image1.png', 'image2.png', 'image3.png']  
  
# 循环遍历每张图片  
for image in images:  
    # 等待一段时间,以便图像加载  
    time.sleep(2)  
      
    # 使用 pyautogui.locateOnScreen() 函数查找图像  
    location = pyautogui.locateOnScreen(image)  
      
    # 如果找到了图像,则输出图像的位置  
    if location is not None:  
        print(f"Found image at {location}")  
    else:  
        print(f"Could not find image at {image}")

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
你可以使用pyautogui.locateAllOnScreen()方法来查找多个图像并获取其位置。这个方法返回一个生成器对象,你可以对它进行循环迭代并获取所有匹配的位置。

下面是一个实现的示例代码:

import pyautogui

# 定义要查找的图像列表
image_list = ['Royal.png', 'Royal2.png', 'Royal3.png']

# 设置查找区域
search_region = (50, 55, 310, 230)

# 用于存储匹配的位置信息
matches = []

# 遍历所有图像并查找它们的位置信息
for image in image_list:
    match = pyautogui.locateCenterOnScreen(image, grayscale=True, confidence=0.5, region=search_region)
    if match is not None:
        matches.append(match)

# 判断是否有匹配的位置信息
if len(matches) > 0:
    print('我想要的效果:', matches)
else:
    print('未找到任何匹配的图像!')

这个代码首先定义了要查找的图片列表(在本例中是['Royal.png', 'Royal2.png', 'Royal3.png']),然后设置了搜索区域(即屏幕上要搜索的特定区域)。接下来,它遍历所有图片并使用pyautogui.locateCenterOnScreen()方法来搜索它们的位置。 如果找到匹配,就将该位置信息添加到列表中。

最后,如果有任何匹配,则输出匹配的位置信息列表;否则,输出一个未找到任何匹配的图像的消息。

请注意,在实际使用过程中,你可能需要调整一些参数(例如,confidence参数可以控制查找的精度,你可以根据实际情况进行调整)来获取最佳的结果。

希望能够对解决你的问题有所帮助!
如果我的回答解决了您的问题,请采纳!