Python爬蟲學習筆記(圖形驗證碼的識別)

將驗證碼圖片命名code.jpg放在項目根目錄下

import tesserocr
from PIL import Image

image = Image.open('code.jpg') #打開驗證碼圖片
# 模式L”爲灰色圖像,它的每個像素用8bit表示,0表示黑,255表示白,其他數字表示不同的灰度。
image1=image.convert('L') #
threshold = 150 #設置灰度閾值自己調整
table = []
for i in range(256):
    if i < threshold :
        table.append(0)
    else:
        table.append(1)

#二值化
image2 = image1.point(table,'1')
#image2.show()

#image1.show() #顯示轉化結果
result = tesserocr.image_to_text(image2)
print(result)
#print(tesserocr.file_to_text('code.jpg'))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章