LoginSignup
2
2

More than 5 years have passed since last update.

PythonでPDFのHTTPレスポンスからサムネイルを生成する

Last updated at Posted at 2019-03-08

概要

wand(ImageMagick)を使ってPDFの1ページ目をもとにサムネイルを生成するプログラムを作りました。

実行環境

  • Python 3.7.2 64bit
  • MacOS Mojave 64bit

必要ライブラリ、アプリケーション

Wand を使っているので、WandとImageMagickをインストールする必要がある。

$ brew install GhostScript
$ brew install ImageMagick
# pip install Wand

コード

from wand.image import Image as wandImage

def genThumb(response, thumb_width, thumb_height):
    data = response.read()

    # 高速化のためにpdf[0]で1ページ目のみ読み込む 
    image = wandImage(blob=data, format="pdf[0]")

    # 解像度を変更し、pngフォーマットに変換する
    image.resize(width=thumb_width, height=thumb_height)
    temp = image.make_blob(format="png")

    return temp
2
2
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
2
2