You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pyautogui的locateCenterOnScreen无法定位屏幕图片求助

PyAutoGUI无法定位屏幕图片的问题排查与解决

问题场景

使用PyAutoGUI的locateCenterOnScreen()方法获取Chrome图标坐标时,抛出ImageNotFoundException,无法找到目标图片。

问题代码

import pyautogui
pyautogui.FAILSAFE = False

pyautogui.moveTo(0, 0, duration=0)
# pyautogui.click(x=650, y=850, clicks=1, interval=0, button='left', duration=1) # Chrome
x, y = pyautogui.locateCenterOnScreen("resources/chrome.png")
pyautogui.click(x=x, y=y, clicks=1, interval=0, button='left', duration=1)

相关资源

  • 目标查找图片(Chrome图标):
    Chrome图标
  • macOS屏幕截图:
    macOS屏幕截图

报错信息

>>> pyautogui.locateCenterOnScreen("resources/chrome.png")
Traceback (most recent call last):
  File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyautogui/__init__.py", line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyautogui/__init__.py", line 204, in locateCenterOnScreen
    return pyscreeze.locateCenterOnScreen(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyscreeze/__init__.py", line 447, in locateCenterOnScreen
    coords = locateOnScreen(image, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyscreeze/__init__.py", line 405, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyscreeze/__init__.py", line 383, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyscreeze/__init__.py", line 371, in _locateAll_pillow
    raise ImageNotFoundException('Could not locate the image.')
pyscreeze.ImageNotFoundException: Could not locate the image.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyautogui/__init__.py", line 174, in wrapper
    raise ImageNotFoundException  # Raise PyAutoGUI's ImageNotFoundException.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pyautogui.ImageNotFoundException

解决方法

1. 验证图片路径

先确认目标图片路径正确,用绝对路径排查文件是否存在:

import os
image_path = os.path.abspath("resources/chrome.png")
if not os.path.exists(image_path):
    print("图片文件不存在,请检查路径")

2. 适配Retina屏幕

MacOS Retina屏幕的截图分辨率是实际显示的2倍,需保证目标图片是从当前屏幕截取的,或者限定查找范围缩小检索区域:

# 限定查找区域为屏幕底部Dock区域(可根据自己屏幕尺寸调整参数)
x, y = pyautogui.locateCenterOnScreen("resources/chrome.png", region=(0, 800, 1920, 200))

3. 降低匹配精度

安装opencv-python后,通过confidence参数降低匹配精度,允许微小像素差异:

pip install opencv-python
x, y = pyautogui.locateCenterOnScreen("resources/chrome.png", confidence=0.8)

4. 检查图片一致性

确保目标图片与屏幕上的图标尺寸、像素完全一致,避免截图时的拉伸、压缩或颜色偏差。

内容的提问来源于stack exchange,提问作者Victor Cordeiro Costa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 23:20:12