image_gray: np.ndarray
TM_METHOD=cv2.TM_CCOEFF_NORMED
def __init__(self):
self.take_screenshot()
def take_screenshot(self):
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
image = ImageGrab.grab()
# save on file
# image.save('screenshot.jpg', format='JPEG', quality=20)
# load image from memory
image_rgb = np.array(image)
# load from file
# image_rgb=cv2.imread('screenshot.jpg')
self.image_gray = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)
@staticmethod
def save_screenshot():
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
image = ImageGrab.grab()
image.save("screenshot.jpg")
def template_available(self, template_path: str, threshold=0.9):
# self.take_screenshot()
template = cv2.imread(template_path, 0)
# w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image_gray, template, self.TM_METHOD)
loc = np.where(res >= threshold)
if len(loc[0]) == 0 & len(loc[1]) == 0:
return False
else:
print("template_available : " + template_path)
return True
def template_count(self, template_path: str, threshold=0.9):
# self.take_screenshot()
template = cv2.imread(template_path, 0)
# w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image_gray, template, self.TM_METHOD)
loc = np.where(res >= threshold)
return len(loc[0])
def template_location(self, template_path: str, threshold=0.9):
self.take_screenshot()
template = cv2.imread(template_path, 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image_gray, template, self.TM_METHOD)
loc = np.where(res >= threshold)
if len(loc[0]) == 0 & len(loc[1]) == 0:
return None
else:
click_y = loc[0][0] + h / 2
click_x = loc[1][0] + w / 2
return click_x, click_y
def move_mouse_to_template(self, template_path: str):
loc = self.template_location(template_path)
if loc is not None:
click_x = loc[0]
click_y = loc[1]
pyautogui.moveTo(click_x, click_y)
def click_template(self, template_path: str, threshold=0.9):
loc = self.template_location(template_path, threshold)
if loc is not None:
click_x = loc[0]
click_y = loc[1]
pyautogui.click(click_x, click_y)
pyautogui.FAILSAFE = False
pyautogui.moveTo(0, 0)
print("click_template : " + template_path)
return True
return False
@staticmethod
def scroll_mouse(value=10):
pyautogui.scroll(value)
pyautogui.FAILSAFE = False
pyautogui.moveTo(0, 0)
@staticmethod
def get_template_path(template: str):
template_name = f"template{template}.jpg"
template_path = os.path.join(os.getcwd(), 'templates', template_name)
return template_path