【问题标题】:How can pyautogui.pixelMatchesColor not work properly?pyautogui.pixelMatchesColor 怎么不能正常工作?
【发布时间】:2020-01-03 18:38:29
【问题描述】:
def perfect_click(name):    # Performs a click after checking for loaded image
    location = None
    image_file = name

    while location is None:
        try:
            location = pyautogui.locateOnScreen(image_file)
        except Exception as e:
            print(e)

    coordinates = pyautogui.center(location)
    x, y = coordinates
    print(coordinates)
    pixel = False
    while pixel is False:
        try:
            pixel = pyautogui.pixelMatchesColor(coordinates, (241, 63, 83), tolerance=10)
        except Exception as e:
            print(e)
    pyautogui.click(coordinates)

我正在尝试创建一个函数来检查按钮并等待按钮可单击然后单击它并完成此操作我尝试使用像素匹配并且不可单击的按钮只是颜色较浅的阴影.. 至少在我的情况下,在屏幕上定位并没有检测到它。

这是错误:

pixelMatchesColor() missing 1 required positional argument: 'expectedRGBColor'

我知道你的反应会是什么“正如它所说的那样 它缺少 'expectedRGBColor'"

但问题是即使在按钮匹配后它也不会退出循环。 我尝试过容忍它也不起作用。

我运行程序来打印坐标,但它不是只在这个函数中打印 x 和 y 坐标,在其他函数中工作得很好,它只是循环直到找到图像并单击它。 缺少坐标不会出错

谁能帮我解决这个问题?

【问题讨论】:

    标签: python python-3.x colors pyautogui pixelmatch


    【解决方案1】:

    pixelMatchesColor 正在寻找(x, y, expectedRGBColor, tolerance)

    您的 coordinates 变量为 2 分,但您仅将其放在第一个参数中。您在x, y = coordinates 中分配的xy 将非常适合pixelMatchesColor

    试试这个

    pixel = pyautogui.pixelMatchesColor(x, y, (241, 63, 83), tolerance=10)
    

    【讨论】:

    • 我尝试了你的解决方案@Matt M,但它现在给了我这个错误:
    • argument 2: <class 'TypeError'>: Don't know how to convert parameter 2
    • 这很奇怪,我自己试过这个。在错误行之前尝试print(x)print(y),看看它输出了什么。此外,您可以尝试co1 = coordinates[0]co2 = coordinates[1] 然后...pixelMatchesColor(co1, co2, (241.... 看看是否有效。也可以在错误行之前尝试print(coordinates),让我知道你得到了什么
    • 我之前尝试过 print(coordinates) .. 没有返回任何内容。
    • argument 2: <class 'TypeError'>: Don't know how to convert parameter 2 Point(x=333, y=649)
    【解决方案2】:

    只是提到location = pyautogui.locateOnScreen()函数
    本身会返回一个numpy.intc(你可以在 type(x) 中查看它)。
    要制作pyautogui.pixelMatchesColor(),您需要将numpy.intc 转换为int

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 2019-01-04
      • 2020-09-03
      • 2016-10-10
      • 2016-10-24
      相关资源
      最近更新 更多