图像_pytesseract

所需模块

①安装PIL:pip install Pillow(之前的博客中有写过)

②安装pytesser3:pip install pytesser3

③安装pytesseract:pip install pytesseract

④安装autopy3:

先安装wheel:pip install wheel

下载autopy3-0.51.1-cp36-cp36m-win_amd64.whl【点击打开链接】

执行命令:pip install E:360安全浏览器下载autopy3-0.51.1-cp36-cp36m-win_amd64.whl

##使用pip install autopy3时会报错如下:

④安装Tesseract-OCR:百度直接搜索Tesseract-OCR下载即可,Windows环境安装tesseract-ocr 4.00并配置环境变量

64位的安装包http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe

简体字识别包:https://raw.githubusercontent.com/tesseract-ocr/tessdata/4.00/chi_sim.traineddata

这里要说明的是安装Tesseract-OCR后,其不会被默认添加至环境变量path中,已导致如下报错:

解决办法有两种:(先找到Tesseract-OCR安装文件夹,再找到tesseract.exe文件)

我这里的绝对路径是:D:pythonTesseract-OCR esseract.exe,环境变量路径则是:D:pythonTesseract-OCR

①将此路径添加至环境变量path中(不过我是这么做的,但是PyCharm仍旧报错)

②找到pytesseract.py文件

我这里是C:UsersadminAppDataLocalProgramsPythonPython36Libsite-packagespytesseractpytesseract.py

将文件中的tesseract_cmd修改为上方的绝对路径

打开命令终端,输入:tesseract -v,可以看到版本信息

实例演示

进入正题,如何识别图像中文字

上原图:(这句是海上钢琴师中的一句经典台词)

接下来我们要通过python的pytesseract来识别图片中的字符了

#   _*_ coding:utf-8 _*_
 
import pytesseract
from PIL import Image
 
__author__ = 'admin'
 
im = Image.open(r'C:UsersadminDesktopexample.png')
print(pytesseract.image_to_string(im))

 如果报错:

则修改代码如下:

import pytesseract
from PIL import Image

tessdata_dir_config = '--tessdata-dir "C:\Program Files (x86)\Tesseract-OCR\tessdata"'
im = Image.open(r'./tupian.png')

print(pytesseract.image_to_string(im, config=tessdata_dir_config))

 如果要显示中文:

from PIL import Image
import pytesseract
text = pytesseract.image_to_string(Image.open('./XLF5G7F6SHFCO4G0.jpg'),lang='chi_sim')
print(text)

结束

原文地址:https://www.cnblogs.com/fmgao-technology/p/10177382.html