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
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    操作系统--怎么实现中断
    操作系统中的描述符和GDT
    使用汇编语言实现memcpy
    Redis命令之setbit
    java webService 服务端的创建 和 客户端代码的生成与使用
    springbooot 项目读取yml配置文件(自定义)
    java自定义注解的使用(校验)
    mysql使用存储过程快速插入百万条数据
    springboot项目集成Mybatisplus配置和使用
    mysql 创建函数的过程和使用
  • 原文地址:https://www.cnblogs.com/zhangxiaomeng1991/p/8298280.html
Copyright © 2011-2022 走看看