알뜰살뜰 정보/Python

Python pyautogui 계산기 이미지가 None 일 때

쉬고싶은 거북이 2020. 6. 30. 01:04
기존의 소스를 사용하여 를 계산기에서 캡처 후 사용하였으나
이미지 파일을 켜놓았을 경우 해당 이미지를 찾으나 계산기의 버튼은 누르지 않았다.

>>> import pyautogui >>> five_btn = pyautogui.locateOnScreen('five.png') >>> print(five_btn)

>>> five_btn = pyautogui.locateOnScreen('five.png') >>> center = pyautogui.center(five_btn) >>> print(center)


>>> center= pyautogui.locateOnScreen('five.png') >>> pyautogui.click(center)





import pyautogui

five_btn = pyautogui.locateOnScreen('five.png'confidence=0.9)
print(five_btn)


#이미지 영역의 가운데 위치 얻기
five_btn = pyautogui.locateOnScreen('five.png'confidence=0.9)
center = pyautogui.center(five_btn)
print(center)


#클릭하기
center = pyautogui.locateCenterOnScreen('five.png'confidence=0.9)
pyautogui.click(center)

픽셀이 깨져서 이미지를 가져오지 못했다. pip install opencv-python 을 통해 해결하였다.


참고


The optional keyword argument specifies the accuracy with which the function should locate the image on screen. This is helpful in case the function is not able to locate an image due to negligible pixel differences:confidence

>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('calc7key.png', confidence=0.9)
>>> button7location
Box(left=1416, top=562, width=50, height=41)

Note: You need to have OpenCV installed for the keyword to work.confidence