1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PyOCRを使ったOCRのサンプルコード

Last updated at Posted at 2022-02-13

PyOCRを使ったOCRのサンプルコードです。

ocr.JPG

#####ライブラリのインストール

pip install pyocr #Python用OCRライブラリ
pip install tesseract #GoogleのOCRエンジン ※再起動必要
pip install pdf2image #PDFを画像ファイルに変換
pip install pillow #画像処理用ライブラリ

#####OCRの実行

from PIL import Image #画像処理ライブラリ
import sys #システムパラメータ
import pyocr
import pyocr.builders
import pdf2image

# OCRツール探索(ここは決まり文句)
tools = pyocr.get_available_tools()
if len(tools) == 0:
    print("No OCR tools found")
    sys.exit(1)

# 推奨使用順序(ここは決まり文句)
tool = tools[0]
print("Will use tool '%s'" % (tool.get_name()))

# PDFから画像に変換
images = pdf2image.convert_from_path("sample.pdf", dpi=200, fmt='jpg')
lang = 'jpg' #言語指定

# 画像からテキストに変換
for image in images:
    txt = tool.image_to_string(
        image,
        lang=lang,
        builder=pyocr.builders.TextBuilder()
    )
    print(txt)

##参考
https://gitlab.gnome.org/World/OpenPaperwork/pyocr

##参考
YouTubeで「Pythonを使った事務処理の効率化」と言うタイトルでPyOCRを紹介。

1
4
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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?