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
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    初试 spring web mvc
    读取网络数据缓存在本地 流程图
    servlet 过滤器实现 请求转发(跳转);跨域转发请求;tomcat 环境下。
    C# .net基于Http实现web server(web服务)
    微信公众平台开发
    Linux目录结构及作用
    MySQL事件调度器event的使用
    MySQL触发器trigger的使用
    存储过程的查、改、删
    MySQL游标的简单实践
  • 原文地址:https://www.cnblogs.com/zhangxiaomeng1991/p/8298280.html
Copyright © 2011-2022 走看看