python简单验证码识别

来源:互联网 发布:游戏编程要学java 编辑:程序博客网 时间:2024/05/20 05:30

各种代码网上都有 但是就是寸步难行 抄抄抄 搞起

python3以上不友好支持pytesser

使用pytesseract+Tesseract-OCR+PIL就完美了

pytesseract在使用中会提示找不到文件,是因为tesseract路径没有配置对,找到一个tesseract_cmd的将自己Tesseract-OCR路径添加进去就好了


代码就照抄吧

方法一:可能不能识别小部分图片

import pytesseractfrom PIL import Imageimage = Image.open(r'C:\Users\Administrator\Desktop\dad.png')cstr= pytesseract.image_to_string(image)print(cstr)
方法二:叫啥灰度二值化的 反正我这个小白也不懂
from PIL import Imageimport pytesseract# 二值化threshold = 140table = []for i in range(256):    if i < threshold:        table.append(0)    else:        table.append(1)        # 由于都是数字# 对于识别成字母的 采用该表进行修正rep = {'O': '0',       'I': '1', 'L': '1',       'Z': '2',       'S': '8'       };def getverify1(name):    # 打开图片    im = Image.open(name)    # 转化到灰度图    imgry = im.convert('L')    # 保存图像    imgry.save('g' + name)    # 二值化,采用阈值分割法,threshold为分割点    out = imgry.point(table, '1')    out.save('b' + name)    # 识别    text = pytesseract.image_to_string(out)    # # 识别对吗    # text = text.strip()    # text = text.upper();    # for r in rep:    #     text = text.replace(r, rep[r])    #     out.save(text+'.jpg')    print(text)    return textgetverify1('aa.png')

第二中方法#识别对吗下面这几行去掉#号不能正确识别,加上又可以了 

反正这种很简单的识别满足基本需要 不完美

原创粉丝点击