pixelMatchesColor函数中的参数问题

问题描述

在使用yautogui.locateCenterOnScreen("./saveData/edit1.jpg",minSearchTime=2,confidence=0.8)后,再使用pyautogui.pixelMatchesColor(a[0],a[1],(255,255,0),10)时出现报错。

代码段落如下:

a=pyautogui.locateCenterOnScreen("./saveData/edit1.jpg",minSearchTime=2,confidence=0.8)
if not a:
    return
if (pyautogui.pixelMatchesColor(a[0],a[1],(255,255,0),10)):
    XXXXXXXX

报错如下:

Traceback (most recent call last):
  File "D:\python\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "D:\python\Lib\threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "D:\python\Lib\site-packages\keyboard\_generic.py", line 58, in process
    if self.pre_process_event(event):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\python\Lib\site-packages\keyboard\__init__.py", line 218, in pre_process_event
    callback(event)
  File "D:\python\Lib\site-packages\keyboard\__init__.py", line 649, in <lambda>
    handler = lambda e: (event_type == KEY_DOWN and e.event_type == KEY_UP and e.scan_code in _logically_pressed_keys) or (event_type == e.event_type and callback())
                                                                                                                                                          ^^^^^^^^^^
  File "D:\Desktop\another\XXXX\user\test.py", line 10, in allStart
    if(pyautogui.pixelMatchesColor(a[0],a[1],(255,255,0),10)):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\python\Lib\site-packages\pyscreeze\__init__.py", line 589, in pixelMatchesColor
    pix = pixel(x, y)
          ^^^^^^^^^^^
  File "D:\python\Lib\site-packages\pyscreeze\__init__.py", line 608, in pixel
    color = windll.gdi32.GetPixel(hdc, x, y)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ctypes.ArgumentError: argument 2: TypeError: Don't know how to convert parameter 2

原因分析:

>>>a=pyautogui.locateCenterOnScreen("./saveData/edit1.jpg",minSearchTime=2,confidence=0.8)

>>> type(a[0])
<class 'numpy.int64'>
>>> type(875)
<class 'int'>
>>> type(int(a[0]))
<class 'int'>

如上:问题在于pyautogui.locateCenterOnScreen()函数返回的值的类型是“numpy.int64”,而pyautogui.pixelMatchesColor()函数识别不了这种类型。


解决方案:

手动转换参数类型即可

a=pyautogui.locateCenterOnScreen("./saveData/edit1.jpg",minSearchTime=2,confidence=0.8)
if not a:
    return
if(pyautogui.pixelMatchesColor(int(a[0]),int(a[1]),(255,255,0),10)):
    XXXXXXXX

OK,改完后成功运行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧炎火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值