使用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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2018-01-16 19:02  巅峰之斗  阅读(330)  评论(0编辑  收藏  举报