Reputation: 73
How would you do pyautogui.locateOnScreen()
for multiple images a bunch of images?
Upvotes: 3
Views: 6098
Reputation: 1
An improved version of the implementation by @Afro_Jello. To help if the image isn't showing:
def get_coordinates(images, max_tries=3):
import pyautogui
from contextlib import suppress
# Resolve retries to wait the image been display
while max_tries:
max_tries -= 1
# Capture your screen only once.
haystack = pyautogui.screenshot()
# Loop through multiple images
for needle in images:
# Get coordinates of image within screen capture
with suppress(pyautogui.ImageNotFoundException):
return pyautogui.locate(needle, haystack)
sleep(1)
raise TimeoutError("Images not found!")
Upvotes: -1
Reputation: 21
def get_coordinates(images):
# Capture your screen only once.
haystack = pyautogui.screenshot()
# Loop through multiple images
for needle in images:
# Get coordinates of image within screen capture
return pyautogui.locate(needle, haystack)
print("Images not found!")
Use the screenshot func to capture your screen and then loop through the different images to find a match.
To optimize see optimize
Upvotes: 1
Reputation: 1090
Try this out:
import os
import pyautogui as py
image_list = []
# Get list of all files in current directory
directory = os.listdir()
# Find files that end with .png or .jpg and add to image_list
for file in directory:
if file.endswith('.png') or file.endswith('.jpg'):
image_list.append(file)
# Loop through list to find all the images
for image in image_list:
print(image)
print(py.locateOnScreen(image))
This question is similar to another one, I posted the same answer in both places.
Upvotes: 1