1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import pyautogui import time import numpy as np import cv2 as cv
pyautogui.PAUSE = 0.02
def match_click(image, templ): assert image is not None, "file could not be read, check with os.path.exists()" ret,img = cv.threshold(image,240,255,cv.THRESH_BINARY) template = cv.imread(templ,cv.IMREAD_GRAYSCALE) assert template is not None, "file could not be read, check with os.path.exists()" h, w = template.shape[:2] res = cv.matchTemplate(img, template, cv.TM_CCOEFF_NORMED) threshold = 0.8 loc = np.where( res >= threshold) for pt in zip(*loc[::-1]): pyautogui.click(pt[0],pt[1], button='left') for i in range(12): pyautogui.scroll(-100) time.sleep(0.2)
i = 0 time.sleep(1) while(True):
try: img = pyautogui.screenshot() img = cv.cvtColor(np.asarray(img),cv.COLOR_RGB2GRAY) match_click(img, 'heart.jpg') i +=1 print('第{}页'.format(i))
except: print("没有发现目标") break
|