PythonでPIL (Python Imaging Library)を使う
いつかのにゃんこ
Pythonで画像処理をする時はPIL (Python Imaging Library)を使うらしいです。
- 作者: Jan Erik Solem,相川愛三
- 出版社/メーカー: オライリージャパン
- 発売日: 2013/03/23
- メディア: 大型本
- 購入: 1人 クリック: 22回
- この商品を含むブログ (4件) を見る
でもいざ使おうとするとこんな感じのエラーが出ます。
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()
neko ha kawaii desu ne!
memo
Homebrew:Mac用パッケージ管理システム distribute:Python用パッケージ管理システム(?) easy_install:distributeをインストールすることによって使えるようになるコマンド pip:pythonのパッケージ管理システム distrubute->pip setuptools->easy_install ???
- 作者: Mark Lutz,夏目大
- 出版社/メーカー: オライリージャパン
- 発売日: 2009/02/26
- メディア: 大型本
- 購入: 12人 クリック: 423回
- この商品を含むブログ (124件) を見る