wiedii's memo

色々メモリます。

PythonでPIL (Python Imaging Library)を使う

いつかのにゃんこ
f:id:wiedii:20131030234243j:plain


Pythonで画像処理をする時はPIL (Python Imaging Library)を使うらしいです。

実践 コンピュータビジョン

実践 コンピュータビジョン


でもいざ使おうとするとこんな感じのエラーが出ます。
test

from PIL import Image

im = Image.open("cat.jpg")
im.convert('L').show()

python test

$ python test
Traceback (most recent call last):
  File "test", line 4, in <module>
    im.convert("L").show()
  File "/Library/Python/2.7/site-packages/PIL/Image.py", line 679, in convert
    self.load()
  File "/Library/Python/2.7/site-packages/PIL/ImageFile.py", line 189, in load
    d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
  File "/Library/Python/2.7/site-packages/PIL/Image.py", line 385, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

どうやらlibjpegがないとダメっぽいです。
エラーメッセージ見て「なるほど...libjpegが足りないのか...」と判断した風に書きましたが、ググりました。
まずHomebrewでlibjpegをインストールします。
その後distributeというパッケージをインストールし、easy_installというコマンドでpipをインストールします。

distribute_setup.pyをダウンロードして実行します。
Index of /

$ sudo python distrubute_setup.py

pipをインストールします。

$ easy_install pip

PILをインストールする前に、Homebrewでlibjpegをインストールします。

$ brew install libjpeg

PILをインストールします。

$ pip install PIL

終わったらこんな感じの画面が出てくると思います。

    --------------------------------------------------------------------
    PIL 1.1.7 SETUP SUMMARY
    --------------------------------------------------------------------
    version       1.1.7
    platform      darwin 2.7.5 (default, Aug 25 2013, 00:04:04)
                  [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
    --------------------------------------------------------------------
    --- TKINTER support available
    --- JPEG support available
    --- ZLIB (PNG/ZIP) support available
    *** FREETYPE2 support not available
    *** LITTLECMS support not available
    --------------------------------------------------------------------
    To add a missing option, make sure you have the required
    library, and set the corresponding ROOT variable in the
    setup.py script.
    
    To check the build, run the selftest.py script.
    changing mode of build/scripts-2.7/pilconvert.py from 644 to 755
    changing mode of build/scripts-2.7/pildriver.py from 644 to 755
    changing mode of build/scripts-2.7/pilfile.py from 644 to 755
    changing mode of build/scripts-2.7/pilfont.py from 644 to 755
    changing mode of build/scripts-2.7/pilprint.py from 644 to 755
    
    changing mode of /usr/local/bin/pilconvert.py to 755
    changing mode of /usr/local/bin/pildriver.py to 755
    changing mode of /usr/local/bin/pilfile.py to 755
    changing mode of /usr/local/bin/pilfont.py to 755
    changing mode of /usr/local/bin/pilprint.py to 755
Successfully installed PIL
Cleaning up...

JPEG support availableって書いてありますね。
libjpegを入れる前にPILを入れてしまったときは、PILをアンインストールして再度インストールするといいとかなんとか。

では再度実行...

from PIL import Image

im = Image.open("cat.jpg")
im.convert('L').show()

f:id:wiedii:20131031000030j:plain
neko ha kawaii desu ne!



memo

Homebrew:Mac用パッケージ管理システム
distribute:Python用パッケージ管理システム(?)
easy_install:distributeをインストールすることによって使えるようになるコマンド
pip:pythonのパッケージ管理システム
distrubute->pip setuptools->easy_install
???

初めてのPython 第3版

初めてのPython 第3版