zoukankan      html  css  js  c++  java
  • 使用pytesseract识别简单验证码

    from PIL import Image
    import pytesseract
    from pytesseract import *
    
    rep={'O':'0',                           #替换列表
        'I':'1','L':'1',
        'Z':'2',
        'S':'8'
        };
    
    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('C:/Users/asus-pc/Desktop/Captcha.jpg')     #1.打开图片
    im = im.convert('L')                                        #2.将彩色图像转化为灰度图
    binaryImage = im.point(initTable(), '1')                    #3.降噪,图片二值化
    # binaryImage.show()
    
    text = image_to_string(binaryImage, config='-psm 7')
    
    #4.对于识别结果,常进行一些替换操作
    for r in rep:
        text = text.replace(r,rep[r])
    
    #5.打印识别结果
    print(text)
    
    

    别人写的

    from PIL import Image
    import pytesseract
    from pytesseract import *
    
    rep={'O':'0',                           #替换列表
        'I':'1','L':'1',
        'Z':'2',
        'S':'8'
        };
    
    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('C:/Users/asus-pc/Desktop/Captcha.jpg')     #1.打开图片
    im = im.convert('L')                                        #2.将彩色图像转化为灰度图
    binaryImage = im.point(initTable(), '1')                    #3.降噪,图片二值化
    # binaryImage.show()
    
    text = image_to_string(binaryImage, config='-psm 7')
    
    #4.对于识别结果,常进行一些替换操作
    for r in rep:
        text = text.replace(r,rep[r])
    
    #5.打印识别结果
    print(text)


    作者:小潘东
    链接:https://www.jianshu.com/p/365f91aea667
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    2017 年终总结 —— 在路上
    尝试造了个工具类库,名为 Diana
    走近 Python (类比 JS)
    Node.js 异步异闻录
    使用 Node.js 搭建一个 API 网关
    不就是语法和长难句吗—笔记总结Day4
    不就是语法和长难句吗—笔记总结Day3
    不就是语法和长难句吗—笔记总结Day2
    不就是语法和长难句吗—笔记总结Day1
    Kali Day1
  • 原文地址:https://www.cnblogs.com/zhangxiaomeng1991/p/8298280.html
Copyright © 2011-2022 走看看