公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

一直在写东西,有时候会遇到这种需求,就是把图片上的文字拷贝到自己的文章中,所以写了这个小工具。

百度API OCR小工具

 

配合Snipaste使用天衣无缝,所有的东西都在剪切板里交换,即Snipaste截取的图片在剪切板里面,OCR直接识别后生成的文字也在剪切板里面,只要在ctrl+v就可以复制到文章里面了。

百度API OCR小工具

 

Snipaste 截图的快捷键是Ctrl+Q,设置这个OCR的快捷键为Ctrl+shift+O。

百度API OCR小工具

 

通过白嫖百度API的文字识别功能,具体可以参照百度API。

百度API OCR小工具

 

直接上程序

# encoding:utf-8
import requests
import base64
from PIL import Image
from PIL import ImageGrab
import pyperclip
from io import BytesIO
import sys
import win32clipboard as w
import win32con
'''
通用文字识别(高精度版)
'''
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
# 二进制方式打开图片文件
def get_token():
  # client_id 为官网获取的AK, client_secret 为官网获取的SK
  host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxxxxxxx&client_secret=xxxxxxxxxxxxxxxx'
  response = requests.get(host)
  if response:
  	return response.json()['access_token']
def encode_image_from_clip():
  picture_format = 'png'
  image = ImageGrab.grabclipboard()
  if not isinstance(image, Image.Image):
  # tkinter.messagebox.showinfo("picture2base64", "not a image in clipboard.")
  	print("not a image in clipboard.")
  sys.exit(1)
  img_buffer = BytesIO()
  image.save(img_buffer, format=picture_format, optimize=True, quality=40)
  byte_data = img_buffer.getvalue()
  base64_byte = base64.b64encode(byte_data)
  return base64_byte
def setClipboard(Str):
  w.OpenClipboard()
  w.EmptyClipboard()
  w.SetClipboardData(win32con.CF_UNICODETEXT, Str)
  w.CloseClipboard()
img = encode_image_from_clip()
params = {"image":img}
access_token = get_token()
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'Application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
	results = response.json()['words_result']
results_display = ''
for result in results:
	results_display = results_display + result['words'] + 'n' 
print(results_display)
setClipboard(results_display)

然后通过pyinstaller 打包成exe就可以了。

分享到:
标签:OCR
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定