python pytesseract 使用说明

来源:互联网 发布:手游网络加速 编辑:程序博客网 时间:2024/05/21 06:41

1、安装

pip install pytesseract

2、使用

#!/usr/bin/python
import Image
import pytesseract
imageObject=Image.open('test.jpg')
print (imageObject)
print (pytesseract.image_to_string(imageObject))
#print (pytesseract.image_to_string(Image.open('/root/Desktop/pythoncode/test.png')))
#print (open('test.png').read())

运行,出错:

root@TestBackTrack:~/Desktop/pythoncode# python testOcr.py
<PngImagePlugin.PngImageFile image mode=P size=70x15 at 0xB73A47EC>
Traceback (most recent call last):
  File "testOcr.py", line 6, in <module>
    print (pytesseract.image_to_string(imageObject))
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 142, in image_to_string
    config=config)
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 75, in run_tesseract
    stderr=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

首先想到的是:是不是test.jpg找不到?

测试了读文件函数,没有问题

而后网搜了下,发现没有安装tesseract-ocr包

参考:http://stackoverflow.com/questions/18322933/pytesser-simple-usage-error

root@TestBackTrack:~/Desktop/pythoncode# sudo apt-get install tesseract-ocr
Reading package lists... Done
Building dependency tree      
.......

再次运行,可以显示数据

2 0