python验证码图片处理--二值化

版权声明:本文为博主原创文章,未经允许,不得转载,如需转载请注明出处 https://blog.csdn.net/ssjdoudou/article/details/83832502

写在最前面:

这个我打算分几次写,由于我们通过selenium拿到的图片会很模糊,所以使用Tesseract识别之前要对图片先进行处理。

第一步就是二值化,设定阈值,低于阈值全部为白色(置0),其余黑色(置1)。

import pytesseract
from PIL import Image,ImageEnhance

def binaryzation(threshold=145):           #降噪,图片二值化
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)

    return table

image = Image.open('newcode.jpg')          #打开图片
image = image.convert('L')                 #转化为灰度图
image.show()
image = image.point(binaryzation(), '1')   #二值化
image.show()

这是原始图片 :

转化为灰度图:

二值化:

今天先到这儿,我要继续研究啦~

猜你喜欢

转载自blog.csdn.net/ssjdoudou/article/details/83832502