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
參考鏈接: