共計 681 個字符,預計需要花費 2 分鐘才能閱讀完成。
要提取圖片中的文字,可以使用 Python 的 OCR(Optical Character Recognition,光學字符識別)庫。以下是使用 tesseract 庫進行圖片文字提取的示例代碼:
- 首先,確保已經安裝了 tesseract OCR 庫和 Python 的 tesseract 庫。可以使用以下命令進行安裝:
pip install tesseract
pip install pytesseract
- 導入所需的庫:
import pytesseract
from PIL import Image
- 加載并預處理圖片:
image = Image.open('image.jpg') # 加載圖片
image = image.convert('L') # 轉為灰度圖像
- 使用 tesseract 進行文字提取:
text = pytesseract.image_to_string(image)
print(text)
運行以上代碼后,將會輸出提取到的圖片文字。
需要注意的是,以上代碼使用的是 tesseract 的默認語言模型進行文字提取。如果需要提取特定語言的文字,可以使用 pytesseract.image_to_string
函數的 lang
參數指定語言模型,例如:
text = pytesseract.image_to_string(image, lang='chi_sim') # 提取中文文字
此外,要使用 tesseract 進行文字提取,還需要將 tesseract 的可執行程序路徑添加到系統環境變量中,或者在代碼中指定 tesseract 的可執行程序路徑。
丸趣 TV 網 – 提供最優質的資源集合!
正文完