使用平台
windows7python3.6MIX2手机代码原理
手机屏幕内容同步到pc端对问题截图对截图文字分析用浏览器自动搜索文本使用教程
使用Airdroid 将手机屏幕显示在电脑屏幕上。也可使用360手机助手实现。不涉及任何代码。实现效果如图:在提问出现时,运行python程序,将问题部分截图。
这里要用到两个函数:
get_point() #采集要截图的坐标,以及图片的高度宽度window_capture() #截图
def get_point():'''采集坐标,并返回w,h,x,y。 作为window_capture() 函数使用'''try:print('正在采集坐标1,请将鼠标移动到该点')# print(3)# time.sleep(1)print(2)time.sleep(1)print(1)time.sleep(1)x1,y1 = pag.position() #返回鼠标的坐标print('采集成功,坐标为:',(x1,y1))print('')# time.sleep(2)print('正在采集坐标2,请将鼠标移动到该点')print(3)time.sleep(1)print(2)time.sleep(1)print(1)time.sleep(1)x2, y2 = pag.position() # 返回鼠标的坐标print('采集成功,坐标为:',(x2,y2))#os.system('cls')#清除屏幕w = abs(x1 - x2)h = abs(y1 - y2)x = min(x1, x2)y = min(y1, y2)return (w,h,x,y)except KeyboardInterrupt:print('获取失败')
def window_capture(result,filename):'''获取截图'''#宽度w#高度h#左上角截图的坐标x,yw,h,x,y=resulthwnd = 0hwndDC = win32gui.GetWindowDC(hwnd)mfcDC = win32ui.CreateDCFromHandle(hwndDC)saveDC = mfcDC.CreateCompatibleDC()saveBitMap = win32ui.CreateBitmap()MoniterDev = win32api.EnumDisplayMonitors(None,None)#w = MoniterDev[0][2][2]# #h = MoniterDev[0][2][3]# w = 516# h = 514saveBitMap.CreateCompatibleBitmap(mfcDC,w,h)saveDC.SelectObject(saveBitMap)saveDC.BitBlt((0,0),(w,h),mfcDC,(x,y),win32con.SRCCOPY)saveBitMap.SaveBitmapFile(saveDC,filename)
def orc_pic():#识别中文text=pytesseract.image_to_string(Image.open('jietu.jpg'),lang='chi_sim')#识别英文# text=pytesseract.image_to_string(Image.open('jietu.jpg'))text = ''.join(text.split())return text #浏览器搜索url = '/s?wd=%s' % textwebbrowser.open(url)运行后截图如下
3.对图片文字分析提取
参考链接: * 图片转文本 * 配置方式 代码部分:4.对文本进行搜索
所有代码如下:
#coding:'utf-8'import win32gui, win32ui, win32con, win32apifrom PIL import Imageimport pytesseractimport webbrowser#先下载pyautogui库,pip install pyautoguiimport os,timeimport pyautogui as pag#获取sdk /。#获取aip pip install git+/Baidu-AIP/python-sdk.git@masterfrom aip import AipOcrimport jsonstatus=0""" 你的 APPID AK SK """APP_ID = '****'API_KEY = '***'SECRET_KEY = '***'client = AipOcr(APP_ID, API_KEY, SECRET_KEY)""" 读取图片 """def get_question(path):'''百度识别图片文字''' with open(path, 'rb') as fp:image=fp.read()res = client.basicGeneral(image)words = res['words_result']lines = [item['words'] for item in words]question = ''.join(lines)if question[1] == '.':question = question[2:]elif question[2] == '.':question = question[3:]return question.replace('?', ' ')#采集坐标def get_point():'''采集坐标,并返回w,h,x,y。 作为window_capture() 函数使用''' try:print('正在采集坐标1,请将鼠标移动到该点')# print(3) # time.sleep(1) print(2)time.sleep(1)print(1)time.sleep(1)x1,y1 = pag.position() #返回鼠标的坐标 print('采集成功,坐标为:',(x1,y1))print('')# time.sleep(2) print('正在采集坐标2,请将鼠标移动到该点')print(3)time.sleep(1)print(2)time.sleep(1)print(1)time.sleep(1)x2, y2 = pag.position() # 返回鼠标的坐标 print('采集成功,坐标为:',(x2,y2))#os.system('cls')#清除屏幕 w = abs(x1 - x2)h = abs(y1 - y2)x = min(x1, x2)y = min(y1, y2)return (w,h,x,y)except KeyboardInterrupt:print('获取失败')#获取截图def window_capture(result,filename):'''获取截图''' #宽度w #高度h #左上角截图的坐标x,y w,h,x,y=resulthwnd = 0 hwndDC = win32gui.GetWindowDC(hwnd)mfcDC = win32ui.CreateDCFromHandle(hwndDC)saveDC = mfcDC.CreateCompatibleDC()saveBitMap = win32ui.CreateBitmap()MoniterDev = win32api.EnumDisplayMonitors(None,None)#w = MoniterDev[0][2][2] # #h = MoniterDev[0][2][3] # w = 516 # h = 514 saveBitMap.CreateCompatibleBitmap(mfcDC,w,h)saveDC.SelectObject(saveBitMap)saveDC.BitBlt((0,0),(w,h),mfcDC,(x,y),win32con.SRCCOPY)saveBitMap.SaveBitmapFile(saveDC,filename)def get_point_txt(status):#如果status=y,则重新获取坐标 '''如果存在point.txt,则询问是否重新采集,删除point.txt;如果不存在txt,则直接采集。''' if not os.path.isfile('point.txt') :result = get_point()with open('point.txt', 'w') as f:f.write(str(result))return resultelse:if status=='y':result = get_point()with open('point.txt', 'w') as f:f.write(str(result))return resultelse:with open('point.txt', 'r') as f:result = f.readline()result = eval(result)return resultdef orc_pic():#识别中文 text=pytesseract.image_to_string(Image.open('jietu.jpg'),lang='chi_sim')#识别英文 # text=pytesseract.image_to_string(Image.open('jietu.jpg')) text = ''.join(text.split())return text#百度识别def orc_baidu():text=get_question('jietu.jpg')return textstatus='y'start = time.time()result=get_point_txt(status)for i in range(10):window_capture(result,'jietu.jpg')# text=orc_baidu()text=orc_pic()print(text)#浏览器搜索url = '/s?wd=%s' % textwebbrowser.open(url)# url2='/search?q=%s' % text# webbrowser.open(url2)end = time.time()time=end-startprint('此次耗时%.1f秒' % time)
如果觉得《用python搭建 百万答题 自动百度搜索答案》对你有帮助,请点赞、收藏,并留下你的观点哦!