LoginSignup
16
20

More than 3 years have passed since last update.

Python Tesseract + PyOCRを使って画像データのテキスト化

Posted at

Python Tesseract + PyOCRを使って画像データのテキスト化

Pythonで画像データをテキスト化はTesseract + PyOCRを使うことで簡単に実現が可能だった

準備

PyOCRインストール

$ pip install pyocr

Tesseractインストール
※ Windows環境は別方法でインストールしてください

$ brew install tesseract
$ ls /usr/local/Cellar/tesseract/4.1.1/share/tessdata/

jpn.traineddata取得

$ wget https://github.com/tesseract-ocr/tessdata/raw/4.1.0/jpn.traineddata
$ mv jpn.traineddata /usr/local/Cellar/tesseract/4.1.1/share/tessdata/

サンプル

以下の画像をPyOCRを使って解析
スクリーンショット 2021-04-11 23.02.07.png

from PIL import Image
import pyocr
import pyocr.builders


def main():
    # OCRエンジンの取得
    tools = pyocr.get_available_tools()
    tool = tools[0]

    # OCR実行
    builder = pyocr.builders.TextBuilder()

    with Image.open("images/sample.png") as im:
        result = tool.image_to_string(im, lang="jpn", builder=builder)
        print(result)


if __name__ == "__main__":
    main()

出力

ToysCreation

ト イ ズ ク リ エ イ シ ョ ン

簡単に画像データからテキストを出力することができました
いいね!と思ったら LGTM お願いします :clap::clap::clap:

【PR】プログラミング新聞リリースしました! → https://pronichi.com
【PR】週末ハッカソンというイベントやってます! → https://weekend-hackathon.toyscreation.jp/about/

16
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
20