最近不想打字,直接截图然后识别出来文字,不就可以不用打吗?我就是太懒了。
这个库让你可以控制和监控输入设备。
对于每一种输入设备,它包含一个子包来控制和监控该种输入设备:
安装
pip install keyboard==0.9.3
当我按下alt+a就开始截图,注意要登着微信,然后就enter保存
import keyboard
from PIL import ImageGrab
import time
def screen():
print('开始截图')
# 使用微信的截图热键
keyboard.wait('alt+a')
# 保存
keyboard.wait('enter')
# 图片保存需要时间
time.sleep(0.5)
# 读取剪切板的图片
image = ImageGrab.grabclipboard()
# 保存图片
image.save('screen.jpg')
print('图片保存完成')
if __name__ == '__main__':
screen()
效果如下
使用百度云来进行识别
百度的技术,阿里的运营,腾讯的产品,江湖常话。
创建文字识别的应用
官方的教程
https://cloud.baidu.com/doc/OCR/index.html
记得pip install aip
from aip import AipOcr
""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
"""读取图片"""
def get_file_content(filepath):
with open(filepath, 'rb') as f:
return f.read()
def get_img_content(img):
image_content = ''
content = client.basicAccurate(image=img)
# print(content)
for words in content['words_result']:
# print(words) # 字典
image_content += words['words']
print(image_content)
with open('demo.txt','w',encoding='utf-8') as f:
f.write(image_content)
if __name__ == '__main__':
# screen()
img = get_file_content('screen.jpg')
get_img_content(img)
效果如下
一定要在代码最后面加上input(),这样打开exe不会一散而过
执行pyinstaller -F demo.py,搞定
还不如直接复制粘贴,但是如果复制不了就派上用场了
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有