首页 > 技术文章 > python实现中文图片文字识别--OCR about chinese text--tesseract

flyinghorse 2016-08-12 17:27 原文

0.我的环境:

win7 32bits

python 3.5

pycharm 5.0 

 

1.相关库

安装pillow:

pip install pillow

安装tesseract:

tesseract-ocr-setup-3.02.02.exe

自带了英文语言包,如果需要中文语言包往下找即可。

或者在安装的时候,在选项lang处,点选chi-sim即可。

安装完毕后,会儿自动加入系统环境变量中。

安装pytesseract:

pip install pytesseract

 

2.修改pytesseract.py原文件

# tesseract_cmd = 'tesseract'

tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'

#如果不修改,会报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件。

#f = open(output_file_name)

f = open(output_file_name, encoding='utf-8')

#如果不修改,会儿报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xyy in position xxx: illegal multibyte sequence

 

3.小程序,测试一下

 1 #coding:utf-8
 2 #Test one page
 3 import pytesseract
 4 from PIL import Image
 5 
 6 def processImage():
 7     image = Image.open('test.png')
 8 
 9     #背景色处理,可有可无
10     image = image.point(lambda x: 0 if x < 143 else 255)
11     newFilePath = 'raw-test.png'
12     image.save(newFilePath)
13 
14     content = pytesseract.image_to_string(Image.open(newFilePath), lang='eng')
15     #中文图片的话,是lang='chi_sim'
16     print(content)
17 
18 processImage()

 

推荐阅读