首页 > 其他 > 详细

使用pytesseract识别简单验证码

时间:2018-01-16 23:15:51      阅读:318      评论:0      收藏:0      [点我收藏+]
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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

使用pytesseract识别简单验证码

原文:https://www.cnblogs.com/zhangxiaomeng1991/p/8298280.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!