利用pytesser模块实现图片文字识别

pytesser是谷歌OCR开源项目的一个模块,在python中导入这个模块即可将图片中的文字转换成文本。

pytesser  调用了  tesseract。在python中调用pytesser模块,pytesser又用tesseract识别图片中的文字。

下面是整个过程的实现步骤:


这个是免安装的,可以放在python安装文件夹的\Lib\site-packages\  下直接使用

pytesser里包含了tesseract.exe和英语的数据包(默认只识别英文),还有一些示例图片,所以解压缩后即可使用。
可通过以下代码测试:
[python]  view plain copy
  1. >>> from pytesser import *  
  2. >>> image = Image.open('fnord.tif')  # Open image object using PIL  
  3. >>> print image_to_string(image)     # Run tesseract.exe on image  
  4. fnord  
  5. >>> print image_file_to_string('fnord.tif')  
  6. fnord  
[python]  view plain copy
  1. <pre name="code" class="python">from pytesser import *   
  2. #im = Image.open('fnord.tif')   
  3. #im = Image.open('phototest.tif')   
  4. #im = Image.open('eurotext.tif')  
  5. im = Image.open('fonts_test.png')  
  6. text = image_to_string(im)   
  7. print text</pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  
注:该模块需要PIL库的支持。

2、解决识别率低的问题
可以增强图片的显示效果,或者将其转换为黑白的,这样可以使其识别率提升不少:

[python]  view plain copy
  1. enhancer = ImageEnhance.Contrast(image1)  
  2. 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的参数,下面是一个例子:

[python]  view plain copy
  1. """OCR in Python using the Tesseract engine from Google 
  2. http://code.google.com/p/pytesser/ 
  3. by Michael J.T. O'Kelly 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值