#🔒 Issue with Finding PNG Images in a Screenshot using OpenCV and PyAutoGUI

7 messages · Page 1 of 1 (latest)

finite gyro
#

Hello everyone,

I'm working on a Python program that captures screenshots using PyAutoGUI and then uses OpenCV to search for specific PNG images within them. My issue is that the program can only locate the PNG images when I resize the screenshot to 80% or 67% of the actual screen size. At 100% , the program does not find the image.

brazen tapirBOT
#

@finite gyro

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

finite gyro
#

Here is a snippet of my code:

folder_path = "C:/Users/JayB/Downloads/CMD Converter+/res"

target_images = ["dummy.png", "dummy2.png", "dummy3.png", "dummy4.png", "dummy5.png", "dummy6.png"]
#
    max_width = 0
    max_height = 0
    for file_name in os.listdir(folder_path):
        if file_name in target_images:
            image_path = os.path.join(folder_path, file_name)
            with Image.open(image_path) as img:
                width, height = img.size
                max_width = max(max_width, width)
                max_height = max(max_height, height)
    return max_width, max_height

max_width, max_height = get_largest_image_size(folder_path)

def capture_screen_at_mouse():
    mouse_x, mouse_y = pyautogui.position()
    width, height = pyautogui.size()
    left = max(0, mouse_x - max_width // 2)
    top = max(0, mouse_y - max_height // 2)
    right = min(width, mouse_x + max_width // 2)
    bottom = min(height, mouse_y + max_height // 2)
    screenshot = ImageGrab.grab(bbox=(left, top, right, bottom))
    return screenshot```
#
    time.sleep(2)
    screenshot = capture_screen_at_mouse()
    screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
    screenshot_window = tk.Toplevel(root)
    screenshot_window.title("Screenshot")
    for file_name in os.listdir(folder_path):
        if file_name in target_images:
            template_path = os.path.join(folder_path, file_name)
            template = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE)
            if template.shape[0] > screenshot_gray.shape[0] or template.shape[1] > screenshot_gray.shape[1]:
                template = cv2.resize(template, (screenshot_gray.shape[1], screenshot_gray.shape[0]))
            result = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
            min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
            if max_val > 0.8:
                print(f"Ähnlichkeit gefunden mit {file_name} bei {max_loc}")
                break
    img = ImageTk.PhotoImage(screenshot)
    label = tk.Label(screenshot_window, image=img)
    label.image = img
    label.pack()

def show_Game():
    start_button = tk.Button(root, text="Start Game", bg="blue", fg="white", command=show_screenshot)
    start_button.place(x=5, y=50)```
brazen tapirBOT
#

@finite gyro

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.