-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
screenshot.py
65 lines (60 loc) · 1.89 KB
/
screenshot.py
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
63
64
65
# -*- coding: utf-8 -*-
"""
手机屏幕截图的代码
"""
import subprocess
import os
import sys
from PIL import Image
from io import StringIO
try:
from common.auto_adb import auto_adb
except Exception as ex:
print(ex)
print('请将脚本放在项目根目录中运行')
print('请检查项目根目录中的 common 文件夹是否存在')
exit(1)
adb = auto_adb()
# SCREENSHOT_WAY 是截图方法,经过 check_screenshot 后,会自动递减,不需手动修改
SCREENSHOT_WAY = 3
def pull_screenshot():
"""
获取屏幕截图,目前有 0 1 2 3 四种方法,未来添加新的平台监测方法时,
可根据效率及适用性由高到低排序
"""
global SCREENSHOT_WAY
if 1 <= SCREENSHOT_WAY <= 3:
process = subprocess.Popen(
adb.adb_path + ' shell screencap -p',
shell=True, stdout=subprocess.PIPE)
binary_screenshot = process.stdout.read()
if SCREENSHOT_WAY == 2:
binary_screenshot = binary_screenshot.replace(b'\r\n', b'\n')
elif SCREENSHOT_WAY == 1:
binary_screenshot = binary_screenshot.replace(b'\r\r\n', b'\n')
return Image.open(StringIO(binary_screenshot))
elif SCREENSHOT_WAY == 0:
adb.run('shell screencap -p /sdcard/autojump.png')
adb.run('pull /sdcard/autojump.png .')
return Image.open('./autojump.png')
def check_screenshot():
"""
检查获取截图的方式
"""
global SCREENSHOT_WAY
if os.path.isfile('autojump.png'):
try:
os.remove('autojump.png')
except Exception:
pass
if SCREENSHOT_WAY < 0:
print('暂不支持当前设备')
sys.exit()
try:
im = pull_screenshot()
im.load()
im.close()
print('采用方式 {} 获取截图'.format(SCREENSHOT_WAY))
except Exception:
SCREENSHOT_WAY -= 1
check_screenshot()