微信运动自动点赞

思路

通过USB调试在电脑显示并可操作手机屏幕
使用pyautogui获取屏幕图像
使用opencv处理和模板匹配,识别需要点击的区域
使用pyautogui点击,并滚动下一页

方法

安卓手机
  1. 激活开发者模式
  2. 在开发者选项中勾选“允许USB调试”
  3. 用USB线缆连接电脑
电脑
  1. 打开软件scrcpy.exe,正常可看到并操控手机屏幕,软件官网scrcpy
  2. 将窗口调整到固定位置,使用截图工具将待点击的心形图截图保存为heart.jpg
  3. 在同一文件夹创建并运行Python脚本如下
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()"
# 二值化操作,选用了阈值240,如果发现完全不工作,在0-255之间调整一下
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()

# 从PIL转OpenCV
img = cv.cvtColor(np.asarray(img),cv.COLOR_RGB2GRAY)

match_click(img, 'heart.jpg')

i +=1
print('第{}页'.format(i))

except:
print("没有发现目标")
break

存在的Bug

  1. 不能避免给自己点赞,导致进入自己的微信运动主页而中断。