您的位置:首页 > 编程语言 > Python开发

python,使用百度api实现复制截图中的文字

2018-06-03 22:08 1186 查看

百度云文字识别技术文档:

 跳转

第三方模块安装:

  pip install baidu-aip

  pip install Pillow

  pip install keyboard

  pip install pywin32

from aip import AipOcr #百度aip
from PIL import ImageGrab #处理剪切板图片
from PIL import Image
import PIL
import keyboard #监控键盘
import sys
import time,datetime
import random

import win32clipboard as w  # 处理剪切板
import win32con

def screenShot():
'''监控键盘事件,并保存图片'''

# 监控键盘,输入QQ默认截图快捷键时进入
if keyboard.wait(hotkey='ctrl+alt+a') == None:
while True:
time.sleep(3)  #等待截图
im = ImageGrab.grabclipboard()  #获取剪切板中的图片
if isinstance(im,PIL.BmpImagePlugin.DibImageFile):  #若剪切板的内容可以解析成图片
#文件名
i = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
r = str(random.randint(100,1000))
#保存图片
im.save(i+r+'.png')

#百度云账号设置
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
#百度云api对象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

#读取图片
image = get_file_content(i+r+'.png')
#获取图片中的文字内容
data = client.basicGeneral(image)
words_result = data['words_result']
data = ""  # 需要保存的内容
for words in words_result:
data+=words['words']

print(data)
setText(data)

#读取图片
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()

#写入剪切板内容
def setText(aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardText(aString)
w.CloseClipboard()

if __name__ == "__main__":
#for _ in range(sys.maxsize):  #修改成在screenShot中用while循环
screenShot()

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: