龙空技术网

教你用Python实现截图和文字识别,就是这么简单

我是数学天才 1668

前言:

当前小伙伴们对“python截图网页”大致比较看重,各位老铁们都需要了解一些“python截图网页”的相关文章。那么小编在网上搜集了一些有关“python截图网页””的相关文章,希望同学们能喜欢,朋友们快快来了解一下吧!

@Author: By Runsen

keyboard是一个监控键盘输入的库

安装:pip install keyborad

import keyboardimport timefrom PIL import ImageGrabdef screen():    print('开始截图')    # 使用微信的截图热键    keyboard.wait(hotkey='alt+a')    # 保存    keyboard.wait(hotkey='enter')    # 图片保存需要时间    time.sleep(0.5)    # 读取剪切板的图片    image = ImageGrab.grabclipboard()    # 保存图片    image.save('screen.jpg')    print('图片保存完成')screen()

当在键盘敲ctrl+a来得到图片

截取的图片

下面我使用百度云来进行识别

为什么用百度云,因为百度的技术,阿里的运营,腾讯的产品

技术当然选百度云

要安装百度的接口

官方的教程

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)    

下面,封装全代码

# -*- coding:utf-8 -*-# time :2019/5/2 23:02# author: 毛利import keyboardimport timefrom PIL import ImageGrabdef screen():    print('开始截图')    # 使用微信的截图热键    keyboard.wait(hotkey='alt+a')    # 保存    keyboard.wait(hotkey='enter')    # 图片保存需要时间    time.sleep(0.5)    # 读取剪切板的图片    image = ImageGrab.grabclipboard()    # 保存图片    image.save('screen.jpg')# 使用百度云中的文字识别from aip import AipOcr""" 你的 APPID AK SK """APP_ID = ''   #你的账号的idAPI_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)if __name__ == '__main__':    screen()    img = get_file_content('screen.jpg')    get_img_content(img)

使用:

就是这么简单,不知道你学会了没有。

标签: #python截图网页 #pythonclipboard