问题描述
在使用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,改完后成功运行。