【Windows】【Python】PDFファイルを画像化してOpenCVで扱う
【Windows】【Python】PDFファイルを画像化してOpenCVで扱う

【Windows】【Python】PDFファイルを画像化してOpenCVで扱う

info

More than 5 years have passed since last update.

@Kazuhito(高橋 かずひと)【Windows】【Python】PDFファイルを画像化してOpenCVで扱う
  • Python
  • PDF
  • OpenCV
  • pillow
  • pdf2image
11Last updated at 2019-05-31Posted at 2019-05-31

PythonでPDFを画像として取り込みたかったためメモ。

pdf2imageを利用して実現しています。pillowに依存にしているようなため、以下でインストール。

pip install pdf2image pip install pillow

また、PDF閲覧にpopplerライブラリを利用するため、「Poppler for Windows」から、poppler-0.68.0_x86.7z(2019/5/31時点最新版)をダウンロードして適当な場所に解凍。

# !/usr/bin/env python # -*- coding: utf-8 -*- # pyinstaller --onefile pdfcrop.py import os import numpy as np import cv2 as cv import pdf2image def main(): # popplerへの環境変数PATHを一時的に付与 ################################### poppler_path = os.path.join(os.getcwd(), "poppler-0.68.0", "bin") os.environ["PATH"] += os.pathsep + poppler_path # pdf2imageを用いてPDFファイルを画像へ変換 ################################ filepath = os.path.abspath(os.path.dirname(__file__)) filepath = os.path.join(filepath, "pdf", "template.pdf") pdfimages = pdf2image.convert_from_path(filepath) # 読み込んだ画像はRGB形式のためBRGへ変換し画面表示 ######################### cvimage = np.asarray(pdfimages[0]) cvimage = cv.cvtColor(cvimage, cv.COLOR_RGB2BGR) cvimage = cv.resize(cvimage, (480, 640)) cv.imshow("pdf2image sample", cvimage) cv.waitKey(-1) if __name__ == '__main__': main()

以上。

1111comment0

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 upSign upLogin
📎📎📎📎📎📎📎📎📎📎