Fernando Carmo
Fernando Carmo

Reputation: 1

pyautogui doesn't look when use (region=)

Im having an issue using pyautogui in Python to search for an image within a specific screen region. When searching the entire monitor, it works fine, but when specifying a region, the function returns None.

I have already tried searching on the stackoverflow but couldn't find a solution.

To check if the image is actually inside the selected area, I used the following code:

import numpy as np
from PIL import ImageGrab

screenshot = ImageGrab.grab(bbox=(452, 338, 452+150, 338+150))
screenshot.save('region.png')

After analyzing the saved image (region.png), I confirmed that the image I'm looking for is inside the selected region. However, for the search to work, I need to increase the search area 4 or 5 times, or scan the entire screen.

Here is the function I'm using to locate the image and move the cursor to it:


import pyautogui as pg

def hold_down():
    box = pg.locateOnScreen('imgs/loot.png', confidence=0.7, region=(452, 338, 150, 150))
    if box:
        x, y = pg.center(box)
        x = x / 2
        y = y / 2
        pg.moveTo(x, y)

Note: I'm using a MacBook Air M1 and have already considered the pixel scaling factor. The pg.moveTo(x,y) command is working correctly and moving the cursor to the expected locations.

Has anyone encountered this issue or knows how to fix it?

Upvotes: 0

Views: 17

Answers (0)

Related Questions