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

環境

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

参考

http://christina04.hatenablog.com/entry/2017/01/08/120459