码迷,mamicode.com
首页 > 编程语言 > 详细

python实现中文图片文字识别--OCR about chinese text--tesseract

时间:2016-08-12 18:02:07      阅读:1920      评论:0      收藏:0      [点我收藏+]

标签:

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()

 

python实现中文图片文字识别--OCR about chinese text--tesseract

标签:

原文地址:http://www.cnblogs.com/flyinghorse/p/5765788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!