kill 打字系统
我们最近在练习打字,打字程序就是两个框,对照着上面框,在下面框里面打答案。
原理很简单,就是给上面框截图,然后调用腾讯api的ocr识别,然后再用pythonkeyboard在下面打出来
不过我上网查了一下,发现好像没有这种屏幕截图获取内容文章,所以我就手写了一个,图一乐吧
import json,base64
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
from pykeyboard import *
from pymouse import *
from PIL import Image
from PIL import ImageGrab
import time
m = PyMouse()
k = PyKeyboard()
cred = credential.Credential("腾旭api", "腾讯api secret")
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-beijing", clientProfile)
def printtext(time_0):
with open(r'cut.png','rb') as file_object:
file_base64 = str(base64.b64encode(file_object.read()))
pic = file_base64.split("'")
file_base64_0 = pic[1]
req = models.GeneralBasicOCRRequest()
params = {
"ImageBase64":file_base64_0
}
req.from_json_string(json.dumps(params))
resp = client.GeneralBasicOCR(req)
res_text = resp.to_json_string()
res_json = json.loads(res_text)
text = ''
for i in range(len(res_json['TextDetections'])):
text += res_json['TextDetections'][i]['DetectedText']
answer = text.replace(" ", "")
#answer = filter(str.isdigit,answer)
print (answer)
k.type_string(answer,interval=time_0)
k.tap_key(k.enter_key)
def jietu(size_x1,size_y1,size_x2,size_y2):
#size = (53,73,1082,433)
size = (size_x1,size_y1,size_x2,size_y2)
img = ImageGrab.grab(size)
#img.save("dazi/cut.png")
img.save(r'cut.png')
if __name__ == '__main__':
print("kill 打字系统")
print("by MuKuo")
size_x1 = int(input("size_x1 >"))
size_y1 = int(input("size_y1 >"))
size_x2 = int(input("size_x2 >"))
size_y2 = int(input("size_y2 >"))
click_x = int(input("click_x >"))
click_y = int(input("click_y >"))
time_0 = float(input("time >"))
for i in range(100):
jietu(size_x1,size_y1,size_x2,size_y2)
m.click(click_x,click_y)
time.sleep(1)
printtext(time_0)
time.sleep(1)
下面是配到的实时显示鼠标指针所在位置的代码
import os,time
import pyautogui as pag
try:
while True:
print ("kill 打字系统")
print ("by MuKuo")
print("Press Ctrl-C to end")
x,y = pag.position() #返回鼠标的坐标
posStr="Position:"+str(x).rjust(4)+','+str(y).rjust(4)
print (posStr)#打印坐标
time.sleep(0.2)
os.system('cls')#清楚屏幕
except KeyboardInterrupt:
print ('end....')
A lion doesn't concern himself with the opinions of a sheep.