这篇文章主要为大家详细介绍了用 Python 进行简单图像识别验证码, 具有一定的参考价值, 感兴趣的小伙伴们可以参考一下
这是一个最简单的图像识别, 将图片加载后直接利用 Python 的一个识别引擎进行识别
将图片中的数字通过 pytesseract.image_to_string(image) 识别后将结果存入到本地的 txt 文件中
- #-*-encoding:utf-8-*-
- import pytesseract
- from PIL import Image
- class GetImageDate(object):
- def m(self):
- image = Image.open(u"C:\\a.png")
- text = pytesseract.image_to_string(image)
- return text
- def SaveResultToDocument(self):
- text = self.m()
- f = open(u"C:\\Verification.txt","w")
- print text
- f.write(str(text))
- f.close()
- g = GetImageDate()
- g.SaveResultToDocument()
具体想要实现上面的代码需要安装两个包和一个引擎
在安装之前需要先安装好 Python,pip 并配置好环境变量
所有包的安装都是通过 pip 来安装的, 需要在 windows PowerShell 中进行, 并且是在 C:\Python27\Scripts 目录下
1. 第一个包: pytesseract
pip install pytesseract
若是出现安装错误的情况, 安装不了的时候, 可以将命令改为 pip.exe install pytesseract 来安装
若是将 pip 修改为 pip.exe 安装成功后, 那么下文的所有 pip 都需要改为 pip.exe
2. 第二个包: PIL 安装
pip install PIL
若是失败了可以如下修改 pip install PILLOW
3. 安装识别引擎 tesseract-ocr
下载 tesseract-ocr, 进行默认安装
安装完成后需要配置环境变量, 在系统变量 path 后增加 tesseract-ocr 的安装地址 C:\Program Files (x86)\Tesseract-OCR;
一切都安装完成后运行上述代码, 会发现报错, 此时需要
至此结束
来源: http://www.phperz.com/article/18/0216/362993.html