我们在青春里得到爱,又在成长中失去爱。
but,I always love you as before. ----《你曾经爱我》
又是许久未更新,前几天博客域名被我玩坏了,现在重新买了个新域名,但是当我换上之后,发现访问流量全部清空了….也是难受,老博客好不容易被收录,现在又得重新开始,想到这个我顺便把主题也换了~先在这个新博客发首篇博文试试~
准备
需要准备的工具:
手机一部
USB数据线
adb工具
需要用到的Python库:
PIL
matplotlib
time
os
number
安装方法我就不说了~自行解决
流程
- 1.获取手机截图
- 2.获取起点坐标轴和终点坐标轴
- 3.计算起点到终点的长度
- 4.模拟手指按压的时间
获取手机截图
获取截图要用到 adb
工具
如何获取?
获取之前先把手机连接到电脑,并且在开发者模式里面打开USB调试~
这样我们才有权限用adb
执行命令~
连接上之后,先在命令行下用adb
试试~
1
| C:\>adb shell screencap -p /sdcard/screen.png
|
执行这条命令之后打开手机内部存储空间会找到一个名为screen.png
的截图

截图是知道怎么弄了,但是我们得把他拿下来,不然没法定位坐标轴~
1
| C:\>adb pull /sdcard/screen.png
|
这条命令可以把手机内部存储空间里面刚才获取到的截图保存到当前目录下~
Python代码:
1 2 3 4
| import os
os.system("adb shell screencap -p /sdcard/screen.png") # 获取当前手机截图 os.system("adb pull /sdcard/screen.png")
|
这样就实现了流程一的获取手机截图
拿到截图之后,接下来要拿到起点坐标轴和终点坐标轴~
可以用numpy.array
方法把图片返回成二维数组,用matplotlib.pyplot
方法放在坐标轴上
1 2 3 4 5 6 7
| def get_screen_image(self): os.system("adb shell screencap -p /sdcard/screen.png") os.system("adb pull /sdcard/screen.png") return numpy.array(PIL.Image.open("screen.png"))
axes_image = plt.imshow(get_screen_image(),animated=True) plt.show()
|
效果:

现在就可以获取坐标轴了,但是不会凭空获取,我们要用鼠标单击获取,绑定鼠标单击事件,绑定之后还要有鼠标单击之后的回调~
1 2 3 4 5 6 7 8 9 10 11 12
| def get_screen_image(self): os.system("adb shell screencap -p /sdcard/screen.png") os.system("adb pull /sdcard/screen.png") return numpy.array(PIL.Image.open("screen.png"))
def on_calck(self,event, coor=[]): coor.append((event.xdata,event.ydata))
figure = plt.figure() axes_image = plt.imshow(get_screen_image(),animated=True) figure.canvas.mpl_connect("button_press_event",on_calck) # 绑定鼠标单击事件 plt.show()
|
然后再运行一次,点击起点坐标和终点坐标

点了之后,返回结果如下:
1 2 3 4
| E:\Programmer\Python\wechat_jump>python wechat_jump.py 1578 KB/s (58416 bytes in 0.036s) [(368.07142857142844, 1102.8766233766232)] [(368.07142857142844, 1102.8766233766232), (809.62987012986991, 858.72077922077892)]
|
坐标轴就这样拿到手了,流程二搞定,然后就开始跳了~
要怎么跳呢?在刚才那个回调函数中就可以直接把获取到的坐标轴传到跳的方法中来计算起点坐标轴到终点坐标轴的长度~
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
| def get_screen_image(self): os.system("adb shell screencap -p /sdcard/screen.png") os.system("adb pull /sdcard/screen.png") return numpy.array(PIL.Image.open("screen.png"))
def jump_to_next(self,point1,point2): x1, y1 = point1 # 起点位置的X轴和Y轴 x2, y2 = point2 # 终点位置的X轴和Y轴 # 通过勾股定理计算起点坐标到终点坐标的长度 tan = ((x2 - x1)**2 + (y2 - y1)**2)**0.5 # 按压的时间 os.system("adb shell input swipe 320 410 320 410 {}".format(int(tan*1.35)))
def on_calck(self,event, coor=[]): # 获取鼠标单击的X轴和Y轴 coor.append((event.xdata,event.ydata)) # 判断列表里的元素有没有两个,一个是起点位置的X轴和Y轴,另一个是终点位置的X轴和Y轴 # 将起点位置和终点位置的坐标轴计算长度 if len(coor) == 2: print("起点位置: {} 结束位置: {}".format(event.xdata,event.ydata)) jump_to_next(coor.pop(),coor.pop())
figure = plt.figure() axes_image = plt.imshow(get_screen_image(),animated=True) figure.canvas.mpl_connect("button_press_event",on_calck) # 绑定鼠标单击事件 plt.show()
|
到现在为止,已经可以执行跳的操作了,但是不可能跳完一次就结束吧,我们还得让他刷新截图接着跳~
可以直接用matplotlib.animation.FuncAnimation来实现刷新
完整代码:
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| import os import PIL import time import numpy import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation
class Wechat_jump():
def __init__(self): self.need_update = True
# 获取图片/界面 def get_screen_image(self): # 获取当前手机截图 os.system("adb shell screencap -p /sdcard/screen.png") # 从手机里获取图片并保存到电脑当前本地文件夹 os.system("adb pull /sdcard/screen.png") return numpy.array(PIL.Image.open("screen.png")) # 返回一个二维数组
# 计算玄 def jump_to_next(self,point1,point2): x1, y1 = point1 # 起点位置的X轴和Y轴 x2, y2 = point2 # 终点位置的X轴和Y轴 # 通过勾股定理计算起点坐标到终点坐标的长度 tan = ((x2 - x1)**2 + (y2 - y1)**2)**0.5 # 按压的时间,320 410的意思就是手指点在手机屏幕上的位置 os.system("adb shell input swipe 320 410 320 410 {}".format(int(tan*1.35)))
# 鼠标单击回调 def on_calck(self,event, coor=[]): # 获取鼠标单击的X轴和Y轴 coor.append((event.xdata,event.ydata)) # 判断列表里的元素有没有两个,一个是起点位置的X轴和Y轴,另一个是终点位置的X轴和Y轴 if len(coor) == 2: print("起点位置: {} 结束位置: {}".format(event.xdata,event.ydata)) # 将起点位置和终点位置的坐标轴计算长度 self.jump_to_next(coor.pop(),coor.pop()) self.need_update = True
# 更新图片 def update_screen(self,frame): if self.need_update: time.sleep(1) # 重新获取图片 self.axes_image.set_array(self.get_screen_image()) self.need_update = False return self.axes_image, # 返回元组类型
def main(self): figure = plt.figure() # 创建一张空白的图片 #把获取的图片放在坐标轴上~ self.axes_image = plt.imshow(self.get_screen_image(),animated=True) # 绑定鼠标单击事件 figure.canvas.mpl_connect("button_press_event",self.on_calck) # 刷新图片 ani = FuncAnimation(figure,self.update_screen,interval=50,blit=True) plt.show() # 显示
if __name__ == '__main__': w = Wechat_jump() w.main()
|
效果:

源代码: Github
有一份的给打包成exe了~adb包也在里面
百度云盘:百度云 密码: 7jkd