pytesser是谷歌OCR开源项目的一个模块,在python中导入这个模块即可将图片中的文字转换成文本。
pytesser
调用了
tesseract。在python中调用pytesser模块,pytesser又用tesseract识别图片中的文字。
下面是整个过程的实现步骤:
1、首先要在code.google.com下载pytesser。https://code.google.com/p/pytesser/downloads/detail?name=pytesser_v0.0.1.zip
这个是免安装的,可以放在python安装文件夹的\Lib\site-packages\ 下直接使用
pytesser里包含了tesseract.exe和英语的数据包(默认只识别英文),还有一些示例图片,所以解压缩后即可使用。
可通过以下代码测试:
- >>> from pytesser import *
- >>> image = Image.open('fnord.tif') # Open image object using PIL
- >>> print image_to_string(image) # Run tesseract.exe on image
- fnord
- >>> print image_file_to_string('fnord.tif')
- fnord
- <pre name="code" class="python">from pytesser import *
- #im = Image.open('fnord.tif')
- #im = Image.open('phototest.tif')
- #im = Image.open('eurotext.tif')
- im = Image.open('fonts_test.png')
- text = image_to_string(im)
- print text</pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
注:该模块需要PIL库的支持。
2、解决识别率低的问题
可以增强图片的显示效果,或者将其转换为黑白的,这样可以使其识别率提升不少:
- enhancer = ImageEnhance.Contrast(image1)
- image2 = enhancer.enhance(4)
可以再对image2调用 image_to_string识别
3、识别其他语言
tesseract是一个命令行下运行的程序,参数如下:
tesseract imagename outbase [-l lang] [-psm N] [configfile...]
imagename是输入的image的名字
outbase是输出的文本的名字,默认为outbase.txt
-l lang 是定义要识别的的语言,默认为英文
通过以下步骤可以识别其他语言:
(1)、下载其他语言数据包:
将语言包放入pytesser的tessdata文件夹下
接下来修改pytesser.py的参数,下面是一个例子:
- """OCR in Python using the Tesseract engine from Google
- http://code.google.com/p/pytesser/
- by Michael J.T. O'Kelly