前提
pythonでpdfを読み取り、OCRで最終的にはCSVで落としたいと思っております。
※経理の請求書自動照合。どうしてもPDFでしか入手出来ない為
対象画像がtextデータというよりかは
表になっているデータなので
pingに変換⇒画像を読み取り⇒データを抽出としようとしてます。
実現したいこと
pingに変換後、座標からデータをdic型で習得しようとしたのですが
エラーが発生しており、解決できない為アドバイスいただけると幸いです。
発生している問題・エラーメッセージ
AttributeError Traceback (most recent call last) File ~\anaconda3\lib\site-packages\PIL\ImageFile.py:495, in _save(im, fp, tile, bufsize) 494 try: --> 495 fh = fp.fileno() 496 fp.flush() AttributeError: '_idat' object has no attribute 'fileno' During handling of the above exception, another exception occurred: SystemError Traceback (most recent call last) File ~\anaconda3\lib\site-packages\PIL\Image.py:652, in Image._repr_png_(self) 651 try: --> 652 self.save(b, "PNG") 653 except Exception as e: File ~\anaconda3\lib\site-packages\PIL\Image.py:2212, in Image.save(self, fp, format, **params) 2211 try: -> 2212 save_handler(self, fp, filename) 2213 finally: 2214 # do what we can to clean up File ~\anaconda3\lib\site-packages\PIL\PngImagePlugin.py:1348, in _save(im, fp, filename, chunk, save_all) 1347 else: -> 1348 ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)]) 1350 if info: File ~\anaconda3\lib\site-packages\PIL\ImageFile.py:503, in _save(im, fp, tile, bufsize) 502 fp.seek(o) --> 503 e.setimage(im.im, b) 504 if e.pushes_fd: SystemError: tile cannot extend outside image The above exception was the direct cause of the following exception: ValueError Traceback (most recent call last) File ~\anaconda3\lib\site-packages\IPython\core\formatters.py:343, in BaseFormatter.__call__(self, obj) 341 method = get_real_method(obj, self.print_method) 342 if method is not None: --> 343 return method() 344 return None 345 else: File ~\anaconda3\lib\site-packages\PIL\Image.py:654, in Image._repr_png_(self) 652 self.save(b, "PNG") 653 except Exception as e: --> 654 raise ValueError("Could not save to PNG for display") from e 655 return b.getvalue() ValueError: Could not save to PNG for display <PIL.Image.Image image mode=RGB size=145x0 at 0x2B189342880>
該当のソースコード
Python(anaconda環境)
1from PIL import Image 2from glob import glob 3import pyocr 4import os 5import pdf2image 6from PIL import Image 7import sys 8import pyocr.builders 9import pdfminer 10import fitz 11import glob 12from pathlib import Path 13from pdf2image import convert_from_path 14 15pyocr.tesseract.TESSERACT_CMD = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe' 16 17#OCRTEST下にあるpdfファイルを全て取得。一つめのファイルを対処する 18filepaths =[] 19wd = os.getcwd() 20data_dir = os.path.join(wd, "OCRTEST") 21filepaths = glob.glob("{}/**.pdf".format(data_dir), recursive=True) 22filepath = filepaths[0] 23 24# poppler/binを環境変数PATHに追加する 25poppler_dir = Path('__file__').parent.absolute() / "poppler/bin" 26os.environ["PATH"] += os.pathsep + str(poppler_dir) 27pdf_path = Path(filepath) 28pages = convert_from_path(str(pdf_path), 150) 29 30# 画像ファイルを1ページずつ保存 31image_dir = Path("./image_file") 32for i, page in enumerate(pages): 33 file_name = pdf_path.stem + "_{:02d}".format(i + 1) + ".ping" 34 image_path = image_dir / file_name 35 # pingで保存 36 page.save(str(image_path), "PNG") 37 38#出力したPNG画像を取得 39filepaths_j =[] 40wd = os.getcwd() 41data_dir = os.path.join(wd, "image_file") 42filepaths_j = glob.glob("{}/**.ping".format(data_dir), recursive=True) 43filepath_j = filepaths_j[0] 44img = Image.open(filepath_j) 45 46#OCR準備 47tools = pyocr.get_available_tools() 48tool = tools[0] 49tool.get_name() 50 51#座標を指定(ここは外部サイトを利用して座標を指定) 52img_ranges = { 53 # 注番 54 'order_no': (53,843,198,18), 55 # 数量 56 'quantity': (519,841,101,19), 57 # 金額 58 'total_amount': (519,841,101,19), 59} 60crop_imgs = { 61 'order_no': img.crop(img_ranges['order_no']), 62 'quantity': img.crop(img_ranges['quantity']), 63 'total_amount': img.crop(img_ranges['total_amount']), 64} 65 66#確認した所エラーが出る 67crop_imgs['order_no'] 68
試したこと
ある程度調べてはみたのですが
対処法がわからず、試すというほどのものがありません・・。
補足情報(FW/ツールのバージョンなど)
anacondaのjupyter Notebookを使用しております。

あなたの回答
tips
プレビュー