目的是要用pytesser实现验证码识别,首先要准备好工具
- 下载pytesser模块,并导入。导入方法等下说。
- 下载完PIL之后,直接安装即可,系统自动检测python目录
- 在pytesser文件里面就有tesseract.(是否需要再下载tesseract,个人觉得可以不用了吧.)
- 在pytesser文件夹里有个tessdata文件夹,用来存放语言包。语言包下载地址
导入pytesser模块(即为Python添加默认模块搜索 )
以下三种方法选一种,一般使用第三种方法。
1. 动态增加
在代码里添加以下类似代码
import sys
sys.path.append("c:\\")
PS:可用print sys.path里的python默认搜索模块路径
2.修改环境变量
在Windows OS下修改系统环境变量PYTHONPATH
3.添加.pth文件方式
复制pytesser文件夹到/Lib/site-packages目录下,并在/Lib/site-packages目录下添加一个路径文件命名为pytesser.pth,以.pth为后缀,并用记事本在文件里写上想加入的模块文件所在的目录名,即pytesser。(参考本地其他.pth文件)
/Lib/site-packages目录所在位置
1 Windows
c:\python27\Lib\site-packages
2 Linux(ubuntu)
/usr/local/lib/python2.7/dist-packages
3 Linux(redhat)
/usr/lib/python2.7/site-packages
问题
问题一:
from pytesser import *
image = Image.open('F:\\fnord.tif')
s = image_to_string(image)
print s
报错:
> "D:\Python26\python.exe" -u "F:\工作目录\20.python\idscode\menu\menu.py"
Traceback (most recent call last):
File "F:\工作目录\20.python\idscode\menu\menu.py", line 5, in <module>
s = image_to_string(image)
File "D:\Python26\lib\site-packages\pytesser.py", line 31, in image_to_string
call_tesseract(scratch_image_name, scratch_text_name_root)
File "D:\Python26\lib\site-packages\pytesser.py", line 21, in call_tesseract
proc = subprocess.Popen(args)
File "D:\Python26\lib\subprocess.py", line 595, in __init__
errread, errwrite)
File "D:\Python26\lib\subprocess.py", line 821, in _execute_child
startupinfo)
WindowsError: [Error 2]
估计是找不到tesseract.exe
则把tesseract.exe所在目录加在PATH环境变量中,或者把pytesser.py中的tesseract_exe_name的值改为绝对路径
问题二
Traceback (most recent call last):
File "F:\MyPython\clours.py", line 22, in <module>
font = ImageFont.truetype('Arial.ttf', 36)
File "D:\Python27\lib\site-packages\PIL\ImageFont.py", line 218, in truetype
return FreeTypeFont(filename, size, index, encoding)
File "D:\Python27\lib\site-packages\PIL\ImageFont.py", line 134, in __init__
self.font = core.getfont(file, size, index, encoding)
File "D:\Python27\lib\site-packages\PIL\ImageFont.py", line 34, in __getattr__
raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed
先把之前的PIL删了,重新安装一个编译好的PIL
下载地址