Tesseract-OCR識別圖片驗證碼


Tesseract的安裝

windows安裝包:https://digi.bib.uni-mannheim.de/tesseract/,最后一個是最新的。

默認安裝路徑 C:\Program Files (x86)\Tesseract-OCR, 將其添加到系統環境變量Path。

安裝完成之后,在命令行輸入 tesseract -v 測試是否安裝成功。

識別圖片:

tesseract 圖片路徑 輸出文件

識別

1. 直接識別

import pytesseract
from PIL import Image


captcha = Image.open("b.jpg")
result = pytesseract.image_to_string(captcha)
print(result)   #6067

2.二值化處理

二值化處理前必須先轉成灰度圖,然后再設定一個閾值。

import pytesseract
from PIL import Image


def convert_img(img, threshold):
    img = img.convert("L")  # 處理灰度
    pixels = img.load()
    for x in range(img.width):
        for y in range(img.height):
            if pixels[x, y] > threshold:
                pixels[x, y] = 255
            else:
                pixels[x, y] = 0
    return img


captcha = Image.open("e.jpg")
captcha = convert_img(captcha, 150)
captcha.show()
captcha.save("threshold.jpg")
result = pytesseract.image_to_string(captcha)
print(result)  #3n3D

 

 

參考鏈接:

1. Windows安裝Tesseract-OCR 4.00並配置環境變量

2. 小帥b教你如何用python識別圖片驗證碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM