前言闲扯
标题虽然是识别验证码,但实际上本文讲的是OCR(Optical Character Recognition,光学字符识别)。
用到三个模块:
- PIL
- tesseract-oc
- pytesseract
简单介绍下:
1. PIL (Python Imaging Library)是 Python 中最常用的图像处理库;
2. tesseract-oc是HP的一个文字识别项目,后来交给google做了开源;
3. pytesseract是基于tesseract-oc的python封装。
识别
pytesseract的使用十分简单,当然tesseract-oc本身的命令行调用也很简单,基于它的封装用起来也只有一行代码:
from PIL import Image
import pytesseract
im = Image.open("captcha.jpg")
vcode = pytesseract.image_to_string(im)
print(vcode)
运行后不出意外会报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件
方法1: 将tesseract.exe的安装目录添加到PATH环境变量中;
方法2: 修改pytesseract.py文件,指定tesseract.exe安装路径tesseract_cmd = 'D:\\Tesseract-OCR\\tesseract
;