質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PDF

PDF(Portable Document Format)とはISOによって国際標準として制定されている電子ドキュメント用の拡張子です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

0回答

860閲覧

pdfスキャンデータから、画像変換後に文字データを抽出・出力したい。

退会済みユーザー

退会済みユーザー

総合スコア0

PDF

PDF(Portable Document Format)とはISOによって国際標準として制定されている電子ドキュメント用の拡張子です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/10/03 03:03

編集2022/10/03 06:33

前提

手元にある、印刷後にスキャンされたpdfファイルがある。
このファイルから文字を認識させ、文字データとして出力させたい。

実現したいこと

画像変換後に、文字データを抽出する。
できれば、空欄部分に ","を入れてCSV出力したい。

発生している問題・エラーメッセージ

<module 'PIL.Image' from 'C:\\Users\\B09013\\Anaconda3\\lib\\site-packages\\PIL\\Image.py'> --------------------------------------------------------------------------- UnidentifiedImageError Traceback (most recent call last) Input In [22], in <cell line: 45>() 41 tool = tools[0] 43 print(Image) ---> 45 img_org = Image.open(buffer) 46 builder = pyocr.builders.TextBuilder() 47 result = tool.image_to_string(Image.open(buffer), lang="jpn", builder=builder) File ~\Anaconda3\lib\site-packages\PIL\Image.py:3008, in open(fp, mode, formats) 3006 for message in accept_warnings: 3007 warnings.warn(message) -> 3008 raise UnidentifiedImageError( 3009 "cannot identify image file %r" % (filename if filename else fp) 3010 ) UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002316815B3B0>

該当のソースコード

python

1from pdfminer.pdfparser import PDFParser 2from pdfminer.pdfdocument import PDFDocument 3from pdfminer.pdfpage import PDFPage 4from pdfminer.pdfpage import PDFTextExtractionNotAllowed 5from pdfminer.pdfinterp import PDFResourceManager 6from pdfminer.pdfinterp import PDFPageInterpreter 7from pdfminer.pdfdevice import PDFDevice 8from pdfminer.layout import LTPage, LAParams, LTTextBox, LTTextLine, LTImage, LTFigure 9from pdfminer.converter import PDFPageAggregator 10import io 11from PIL import Image 12import pyocr 13 14fp = open("C:\Analysis/test.pdf", "rb") 15 16parser = PDFParser(fp) 17document = PDFDocument(parser) 18rsrcmgr = PDFResourceManager() 19laparams = LAParams() 20device = PDFPageAggregator(rsrcmgr, laparams=laparams) 21interpreter = PDFPageInterpreter(rsrcmgr, device) 22 23for page in PDFPage.create_pages(document): 24 interpreter.process_page(page) 25 layout = device.get_result() 26 27for obj in layout: 28 if isinstance(obj, LTFigure): 29 for obj in obj: 30 if isinstance(obj, LTImage): 31 images = obj 32 33buffer = io.BytesIO() 34buffer.write(images.stream.get_rawdata()) 35buffer.seek(0) 36 37path_tesseract = "C:\Program Files\Tesseract-OCR" 38if path_tesseract not in os.environ["PATH"].split(os.pathsep): 39 os.environ["PATH"] += os.pathsep + path_tesseract 40tools = pyocr.get_available_tools() 41tool = tools[0] 42 43print(Image) 44 45img_org = Image.open(buffer) 46builder = pyocr.builders.TextBuilder() 47result = tool.image_to_string(Image.open(buffer), lang="jpn", builder=builder) 48result

試したこと

Cドライブに管理者権限があるため、ファイルを一度デスクトップに移動させて稼働もさせた。
しかし、かわらなかったため、問題が異なると考えた。

補足情報(FW/ツールのバージョンなど)

pdfには数字が並んでいるだけです。
例: 20:18 2 13:03 1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

can110

2022/10/03 04:20

bufferの画像データフォーマットが認識できないエラーなので、そのbufferをバイナリで保存、画像ビューワーなりで画像形式を確認してその結果を記載ください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問