python识别验证码——一般的数字加字母验证码识别

转自:https://www.cnblogs.com/MrRead/p/7656800.html

 

1、验证码的识别是有针对性的,不同的系统、应用的验证码区别有大有小,只要处理好图片,利用好pytesseract,一般的验证码都可以识别

2、我在识别验证码的路上走了很多弯路,重点应该放在怎么把图片处理成这个样子,方便pytesseract的识别,以提高成功率

3、原图为:

思想过程:

①不要盲目的去直接用代码识别,识别不出来就怀疑代码有问题或者pytesseract不好用:

   先将验证码用图片处理工具处理,一步步得到理想图片,记住处理过程,将处理后的图片直接用pytesseract识别,代码如下:

# -*- coding: UTF-8 -*-、
import Image
import pytesseract
im = Image.open('31.png')
aa = pytesseract.image_to_string(out)
print aa

②确定图片可以识别后,开始用代码复现你的图片处理过程

复制代码
# -*- coding: UTF-8 -*_
from PIL import Image
from pytesseract import *
import PIL.ImageOps
def initTable(threshold=140):
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    return table

im = Image.open('31.png')
#图片的处理过程
im = im.convert('L')
binaryImage = im.point(initTable(), '1')
im1 = binaryImage.convert('L')
im2 = PIL.ImageOps.invert(im1)
im3 = im2.convert('1')
im4 = im3.convert('L')
#将图片中字符裁剪保留
box = (30,10,90,28) 
region = im4.crop(box)  
#将图片字符放大
out = region.resize((120,38)) 
asd = pytesseract.image_to_string(out)
print asd
print (out.show())
复制代码

先将图片转换为L模式

然后去噪

反转颜色

将重要部分裁剪放大

输出结果:

转载于:https://www.cnblogs.com/x_wukong/p/9104041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值