若是你是一个数据挖掘爱好者,那么验证码是你避免不过去的一个天坑,和各类验证码斗争,必然是你成长的一条道路,接下来的几篇文章,我会尽可能的找到各类验证码,而且去尝试解决掉它,中间有些技术甚至我都没有见过,来吧,一块儿Coding吧html
我随便在百度图片搜索了一个验证码,以下 python
pytesseract
解决,它属于Python当中比较简单的
OCR识别库
使用pytesseract
以前,你须要经过pip 安装一下对应的模块 ,须要两个git
pytesseract库还有图像处理的pillow库了github
pip install pytesseract
pip install pillow
复制代码
若是你安装了这两个库以后,编写一个识别代码,通常状况下会报下面这个错误微信
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path
复制代码
这是因为你还缺乏一部份内容app
安装一个Tesseract-OCR软件。这个软件是由Google维护的开源的OCR软件。ide
下载地址 > github.com/tesseract-o…学习
中文包的下载地址 > github.com/tesseract-o…测试
选择你须要的版本进行下载便可网站
命令 | 释义 |
---|---|
open() | 打开一个图片 from PIL import Image im = Image.open("1.png") im.show() |
save() | 保存文件 |
convert() | convert() 是图像实例对象的一个方法,接受一个 mode 参数,用以指定一种色彩模式,mode 的取值能够是以下几种: · 1 (1-bit pixels, black and white, stored with one pixel per byte) · L (8-bit pixels, black and white) · P (8-bit pixels, mapped to any other mode using a colour palette) · RGB (3x8-bit pixels, true colour) · RGBA (4x8-bit pixels, true colour with transparency mask) · CMYK (4x8-bit pixels, colour separation) · YCbCr (3x8-bit pixels, colour video format) · I (32-bit signed integer pixels) · F (32-bit floating point pixels) |
from PIL import Image, ImageFilter
im = Image.open(‘1.png’)
# 高斯模糊
im.filter(ImageFilter.GaussianBlur)
# 普通模糊
im.filter(ImageFilter.BLUR)
# 边缘加强
im.filter(ImageFilter.EDGE_ENHANCE)
# 找到边缘
im.filter(ImageFilter.FIND_EDGES)
# 浮雕
im.filter(ImageFilter.EMBOSS)
# 轮廓
im.filter(ImageFilter.CONTOUR)
# 锐化
im.filter(ImageFilter.SHARPEN)
# 平滑
im.filter(ImageFilter.SMOOTH)
# 细节
im.filter(ImageFilter.DETAIL)
复制代码
format属性定义了图像的格式,若是图像不是从文件打开的,那么该属性值为None; size属性是一个tuple,表示图像的宽和高(单位为像素); mode属性为表示图像的模式,经常使用的模式为:L为灰度图,RGB为真彩色,CMYK为pre-press图像。若是文件不能打开,则抛出IOError异常。
这个地方能够参照一篇博客,写的不错 > www.cnblogs.com/mapu/p/8341…
注意安装完毕,若是仍是报错,请找到模块 pytesseract.py 这个文件,对这个文件进行编辑
通常这个文件在 C:\Program Files\Python36\Lib\site-packages\pytesseract\pytesseract.py
位置
文件中 tesseract_cmd = 'tesseract' 改成本身的地址
例如: tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
复制代码
若是报下面的BUG,请注意
Error opening data file \Program Files (x86)\Tesseract-OCR\tessdata/chi_sim.traineddata Please make sure the TESSDATA_PREFIX environment variable
解决办法也比较容易,按照它的提示,表示缺失了 TESSDATA_PREFIX 这个环境变量。你只须要在系统环境变量中添加一条便可
将 TESSDATA_PREFIX=C:\Program Files (x86)\Tesseract-OCR 添加环境变量
重启IDE或者从新CMD,而后继续运行代码,这个地方注意须要用管理员运行你的py脚本
步骤分为
import pytesseract
from PIL import Image
def main():
image = Image.open("1.jpg")
text = pytesseract.image_to_string(image,lang="chi_sim")
print(text)
if __name__ == '__main__':
main()
复制代码
测试英文,数字什么的基本没有问题,中文简直惨不忍睹。空白比较大的能够识别出来。唉~很差用 固然刚才那个7364
十分轻松的就识别出来了。
接下来识别以下的验证码,咱们首先依旧先尝试一下。运行代码发现没有任何显示。接下来须要对这个图片进行处理
彩色转灰度
im = im.convert('L')
复制代码
灰度转二值,解决方案比较成套路,采用阈值分割法,threshold为分割点
def initTable(threshold=140):
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return table
复制代码
调用
binaryImage = im.point(initTable(), '1')
binaryImage.show()
复制代码
调整以后
tesserocr GitHub:github.com/sirfz/tesse…
tesserocr PyPI:pypi.python.org/pypi/tesser…
pytesserocr GitHub:github.com/madmaze/pyt…
pytesserocr PyPI:pypi.org/project/pyt…
tesseract下载地址:digi.bib.uni-mannheim.de/tesseract
tesseract GitHub:github.com/tesseract-o…
tesseract 语言包:github.com/tesseract-o…
tesseract文档:github.com/tesseract-o…
扫码关注微信公众帐号,领取2T学习资源