1. 程式人生 > >python爬蟲時圖形驗證碼識別( tesserocr)

python爬蟲時圖形驗證碼識別( tesserocr)

window環境下:

第一步:安裝tesseract:教程如下:https://www.cnblogs.com/jianqingwang/p/6978724.html

  

第二步:安裝 tesserocr  : 到這裡下載whl版本  https://github.com/simonflueckiger/tesserocr-windows_build/releases

                   然後在cmd下 pip install D:\Chromedowload\tesserocr-2.2.2-cp36-cp36m-win_amd64.whl

第三步:

import tesserocr

from PIL import Image

ima = Image.open('20180605095750237.png')
res = tesserocr.image_to_text(ima).strip()
print(res)

這樣就可以獲取到驗證碼中的文字了,然後把文字輸入即可。

注意:正常情況下,這樣的識別率是非常低的。需要對圖片進行處理後,再識別。

完整的流程:

import tesserocr
from PIL import Image

image = Image.open('code2.jpg')

image = image.convert('L')  # 轉為灰度影象
threshold = 127  # #指定二值化的閾值
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)

image = image.point(table, '1')  # 進行二值化
image.show()

result = tesserocr.image_to_text(image)
print(result)