溫馨提示×

Python怎么實現識別圖片中的文字

小億
181
2024-01-29 15:01:56
欄目: 編程語言

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的官方文檔。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女