共計 516 個字符,預計需要花費 2 分鐘才能閱讀完成。
Python 中可以使用 OCR(Optical Character Recognition,光學字符識別)庫來實現識別圖片中的文字。下面是一個使用 Tesseract 庫進行 OCR 識別的示例代碼:
import pytesseract
from PIL import Image
# 讀取圖片
image = Image.open('image.png')
# 使用 Tesseract 進行 OCR 識別
text = pytesseract.image_to_string(image, lang='eng')
# 輸出識別的文字
print(text)
在上面的代碼中,首先需要安裝 Tesseract 庫和 Pillow 庫:
pip install pytesseract
pip install Pillow
然后,通過 Image.open
函數打開圖片,并使用 pytesseract.image_to_string
函數將圖片中的文字識別出來。lang
參數可以指定識別的語言,默認為英語。
需要注意的是,使用 Tesseract 進行 OCR 識別需要提前安裝 Tesseract OCR 引擎。具體安裝方法可以參考 Tesseract 的官方文檔。
丸趣 TV 網 – 提供最優質的資源集合!
正文完