环境概述

|Python|3.9.2 |
|-pytesseract 客户端-|-5.0-|
| pytesseract 依赖库| 0.3.8 |
| 语言包|chi_sim.traineddata、chi_sim_vert.traineddata |

下载地址

CSDN资源地址:https://download.csdn.net/download/yanxilou/76285648
客户端:https://github.com/UB-Mannheim/tesseract/wiki
在这里插入图片描述
语言包:https://github.com/tesseract-ocr/tessdata

在这里插入图片描述依赖库: pip install pytesseract

在这里插入图片描述

修改配置

语言包放到这里:
在这里插入图片描述修改cmd路径:
在这里插入图片描述在这里插入图片描述

目录结构

在这里插入图片描述

代码

from PIL import Image
import pytesseract

def read_text(text_path):
  # 打开图片
  im = Image.open(text_path)
  # 转化为8bit的黑白图片
  imgry = im.convert('L')
  # 二值化,采用阈值分割算法,threshold为分割点
  threshold = 140
  table = []
  for j in range(256):
    if j < threshold:
      table.append(0)
    else:
      table.append(1)
  out = imgry.point(table, '1')
  # 识别文本
  text = pytesseract.image_to_string(out, lang="chi_sim", config='--psm 6')
  return text
if __name__ == '__main__':
  print(read_text(r'.\img\demo.png'))

效果

在这里插入图片描述

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐