Pythonで画像内のテキストを認識する方法は何ですか?
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関数を使用して画像内のテキストを認識します。言語パラメーターを指定して、デフォルトは英語です。
Tesseract OCRエンジンを使用したOCR認識には、事前にTesseract OCRエンジンをインストールする必要があります。具体的なインストール方法は、Tesseractの公式ドキュメントを参照してください。