반응형
import pyautogui
#select = pyautogui.locateOnScreen("select.png")
#pyautogui.moveTo(select)
# 속도 개선
# 1. GrayScale 흑백으로 전환해서 속도 개선
# grayscale = True / 이걸로 흑백으로 바꿔서 속도 개선할수 있음.
#select = pyautogui.locateOnScreen("select.png", grayscale=True)
#pyautogui.moveTo(select)
# 2. 범위 지정
# 방법 = pyautogui.locateOnScreen("파일명", region=(x, y, 높이, 가로 넓이)) 여기서 높이는
# 여기서 높이는 아래 범위를 입력, 가로넓이는 가로로 어느 정도 찾을지를 입력
#select = pyautogui.locateOnScreen("select.png", region=(801, 0, 1563-801, 130))
#pyautogui.moveTo(select)
# 3. 정확도 조정, 이거를 하기 위해서는 opencv-python을 깔아야됨
# pip install opencv-python
#play = pyautogui.locateOnScreen("play.png", confidence=0.9) #90% 정확도
#pyautogui.moveTo(play)
# 자동화 대상이 바로 보여지지 않는 경우
# 1. 계속 기다리기
#file_menu = pyautogui.locateOnScreen("file_menu.png")
#if file_menu:
# pyautogui.click(file_menu)
#else:
# print('발견 실패')
#file_menu = None # 이게 없으면 file_menu를 찾을 수 없다고 뜨네!!
#while file_menu is None:
# file_menu = pyautogui.locateOnScreen("file_menu.png")
# print("발견 실패")
#pyautogui.click(file_menu)
# 2. 일정 시간동안 기다리기(TimeOut)
import time
import sys
#timeout = 10 #10초대기
#start = time.time() # 시작 시간 설정
#file_menu = None
#while file_menu is None:
# file_menu = pyautogui.locateOnScreen("file_menu.png")
# end = time.time() # 종료 시간 설정
# if end - start > timeout: # 종료시간 - 시작시간 = (총실행한시간)
# print("시간초과")
# sys.exit()
#pyautogui.click(file_menu)
#함수로 만들기
def find_target(img_file, timeout=30): #find_target 함수 생성
start = time.time() # 현재시간 설정
target = None
while target is None:
target = pyautogui.locateOnScreen(img_file) #여기서 target이 None이 아니게 됨.
end = time.time()
if end - start > timeout:
break
return target #target 값을 돌려줌. 여기서 return 들여쓰기해서 while안에 넣지말자..
def my_click(img_file, timeout=30): # my_click 변수 생성
target = find_target(img_file, timeout) # find_target에 return 받은 target 값을
# 왼쪽 target 값에 저장.
if target: #target이 있다면
pyautogui.click(target)
else: #target이 None 이면
print(f"Timeout {timeout}s Target not found {img_file}, Terminat image_file")
sys.exit()
my_click("file_menu.png", 30) #my_click 함수로 시작
반응형