forked from steveyg/AnswerHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_utils.py
115 lines (98 loc) · 3.59 KB
/
img_utils.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
# -*- coding: utf-8 -*-
from aip import AipOcr
from PIL import ImageGrab
import config
from PIL import Image
import os
import sys
import subprocess
client = AipOcr(config.APP_ID, config.API_KEY, config.SECRET_KEY)
# 通过adb获取android图像
def get_android_img():
os.system('adb shell screencap -p /sdcard/screen.png')
os.system('adb pull /sdcard/screen.png ' + config.IMAGE_PAGE)
im = Image.open(config.IMAGE_PAGE)
im = im.convert('RGB')
im.save(config.IMAGE_PAGE)
# TODO WDA获取图像
def get_ios_img():
img = ImageGrab.grab()
img.save(config.IMAGE_PAGE)
# 投影到桌面进行截图
def get_pc_img(window_cap, box):
if window_cap:
assert sys.platform == 'win32', 'Platform is not Windows'
command = ['windowcap.exe', config.PC_WINDOW_CONFIG]
if not config.PC_WINDOW_FALLBACK:
command.append(config.IMAGE_PAGE)
output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read()
if output:
if output.startswith('('):
img = ImageGrab.grab(eval(output))
img.save(config.IMAGE_PAGE)
else:
raise ValueError(output)
crop(config.IMAGE_PAGE, get_box_by_image(config.IMAGE_PAGE, config.GET_FACTOR))
else:
img = ImageGrab.grab(box)
img.save(config.IMAGE_PAGE_TEMP)
# 裁剪图像
def crop(img_path, box):
img = Image.open(img_path)
# plt.figure("beauty")
# plt.subplot(1, 2, 1), plt.title('origin')
# plt.imshow(img), plt.axis('off')
# TODO 截取区域可以调整
im_crop = img.crop(box)
im_crop.save(config.IMAGE_PAGE_TEMP)
img.close()
def get_box_by_image(img_path, upper_crop_factor):
im = Image.open(img_path)
pixels = im.load()
w, h = im.size
# Count the number of white pixels at the y-axis
# Find the index and last index of pixel count which is greater than or equal to 80% of the screen width
upper = lower = -1
# From top to bottom
for y in range(h):
white_pixel_count = 0
for x in range(w):
if all([c > 240 for c in pixels[x, y]]):
white_pixel_count += 1
if white_pixel_count >= int(0.8 * w):
upper = y
break
# From bottom to top
for y in range(h - 1, -1, -1):
white_pixel_count = 0
for x in range(w):
if all([c > 240 for c in pixels[x, y]]):
white_pixel_count += 1
if white_pixel_count >= int(0.8 * w):
lower = y
break
if upper == -1 or upper == lower:
raise ValueError(u'不能确定图片上的答题区域'.encode(sys.stdout.encoding))
# Cut down a small percent of the box height
upper += upper_crop_factor * (lower - upper)
im.close()
return 0, upper, w, lower
# 百度ocr获取图片位置
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 识别文字
def spot():
if config.GET_DEVICE_TYPE == config.TYPE_PC:
get_pc_img(config.PC_USE_WINDOW_CAPTURE, config.PC_CROP_BOX)
else:
if config.GET_DEVICE_TYPE == config.TYPE_ANDROID:
get_android_img()
elif config.GET_DEVICE_TYPE == config.TYPE_IOS:
get_ios_img()
else:
raise ValueError('Unknown device type')
crop(config.IMAGE_PAGE, get_box_by_image(config.IMAGE_PAGE, config.GET_FACTOR))
image = get_file_content(config.IMAGE_PAGE_TEMP)
return client.basicGeneral(image)