4
2

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 5 years have passed since last update.

wandでpdfを画像形式に変換する

Last updated at Posted at 2017-02-20

環境

mac sierra
python2.7.12
Wand (0.4.4)
imagemagick 6.9.6-8

読むのめんどくさい人用

古いimagemagickを入れて対応した
普通にbrewで入れるとimagemagick-7.0.4-10が入ったが仲が良くない。

インストール

pip install Wand

# brewからimagemagickの古いコミットを探してinstall
cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/
git fetch --unshallow
brew log imagemagick --oneline | grep 6.9.6-8
# 4214c58e6c imagemagick: update 6.9.6-8 bottle.
# ab83029a02 imagemagick 6.9.6-8
git checkout 4214c58e6c imagemagick.rb
brew install imagemagick
# Updating Homebrew...
# ==> Downloading #https://homebrew.bintray.com/bottles/imagemagick-6.9.6-8.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring imagemagick-6.9.6-8.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/imagemagick/6.9.6-8: 1,465 files, 22.2M
git reset HEAD
git checkout .
git fetch --depth=1
git gc
git prune

ソースはこれ

from wand.image import Image

# Converting first page into JPG
with Image(filename="1.pdf[0]") as img:
     img.save(filename="1.jpg")

# 出てたエラー
brew install imagemagick
pip install Wand

pdfから画像に変換したかったのでconvertコマンドを使用しようとしたが、
フォントがないエラーにやる気を削がれる。
http://qiita.com/polikeiji/items/cc0929bc0171b6348f33

wandを使えばいける的な感じの記事を見つける。

いけるのかと思いきやエラー

python pdf2png.py 
Traceback (most recent call last):
  File "pdf2png.py", line 9, in <module>
    from wand.image import Image
  File "/Users/miyamoto/.pyenv/versions/miniconda3-3.19.0/envs/py27/lib/python2.7/site-packages/wand/image.py", line 20, in <module>
    from .api import MagickPixelPacket, libc, libmagick, library
  File "/Users/miyamoto/.pyenv/versions/miniconda3-3.19.0/envs/py27/lib/python2.7/site-packages/wand/api.py", line 206, in <module>
    'Try to install:\n  ' + msg)
ImportError: MagickWand shared library not found.
You probably had not installed ImageMagick library.
Try to install:
  brew install freetype imagemagick


行けないじゃないか。。。
brew install freetype imagemagick
してもうまくいかぬ。

どうやらimagemagickとのバージョンが合わないのではないだろうか
http://stackoverflow.com/questions/25003117/python-doesnt-find-magickwand-libraries-despite-correct-location

ということで古いバージョンを入れたら動いた。

pypdf2とかいうのもあったのをうまくいった後に見つける。
一旦うまくいったのでいいや。
https://gist.github.com/jrsmith3/9947838

参考

brewで過去バージョンをインストールする系
http://christina04.hatenablog.com/entry/2017/01/08/120459

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?