你说什么都对

  • 首页
  • 技术
  • 工具分享
  1. 首页
  2. 技术
  3. Python
  4. 正文

Python 使用Tesseract识别图片中的字符

2022年11月3日 146点热度 0人点赞 0条评论

环境:Python3, Windows

首先先得安装Pillow和pytesseract:

pip install pytesseract
pip install pillow

之后需要在操作系统里安装Tesseract:

  1. 访问https://github.com/UB-Mannheim/tesseract/wiki
  2. 下载tesseract-ocr-w64-setup-v4.1.0.20190314 (rc1)
  3. 安装下载好的tesseract-ocr-w64-setup-v4.1.0.20190314.exe,安装过程中最好选择Chinese(simplified)语言
  4. 安装完毕后,将tesseract的目录(默认“C:\Program Files\Tesseract-OCR”)加入到系统PATH变量中

使用Windows CMD确认安装结果:

C:\Users\xxx>tesseract -v
tesseract v4.0.0.20190314
 leptonica-1.78.0
  libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.3) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.2.0
 Found AVX2
 Found AVX
 Found SSE

C:\Users\xxx>tesseract --list-langs
List of available languages (4):
chi_sim
eng
osd
script/HanS

tesseract-4.0.0a支持以下psm参数:

Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
                        bypassing hacks that are Tesseract-specific.

python3 用法:

from PIL import Image
import pytesseract

img = Image.open('shot.png')
ocr_str = pytesseract.image_to_string(img, lang="eng", config="--psm 7")
print(ocr_str)

如果图片中是纯数字,可以使用:

ocr_str = pytesseract.image_to_string(image, lang='eng', \
        config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
标签: 暂无
最后更新:2022年11月3日

老虎猪

健康、快乐、品质生活

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

最近评论
最新 热点 随机
最新 热点 随机
Jenkins部署 在OneNote里使用markdown语法-OneMark 群晖使用花生壳搭建WordPress bootstrap table 图标显示解决 bootstrap div 居中显示 Django根据字段自动从dict中获取值
自动化测试框架 动态自适应的NavBar - Django Python 使用Tesseract识别图片中的字符 PHP实现图片上传并生成缩略图 自己的Blog Jenkins部署

COPYRIGHT © 2022 你说什么都对. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙公网安备 33022602000616号