1. 程式人生 > >python驗證碼圖片處理--二值化

python驗證碼圖片處理--二值化

寫在最前面:

這個我打算分幾次寫,由於我們通過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()

這是原始圖片 :

轉化為灰度圖:

二值化:

今天先到這兒,我要繼續研究啦~