python のPILで画像処理を試してみる(インストール編)
スポンサーリンク
Raspberry piのWebカメラで取得した情報を色分析とかしていろんなものの、判定できないかなぁと。
夕方になった~、朝になった~、花が咲いた~、花が散った~など。
でぐぐったらこんなありがたいページが。
ライブラリ:PIL Pythonの「PIL」というライブラリについてご紹介します。
ということで、Raspberry piに実装する前にWindows PCでテストした。環境は windows 7(x64) / python 2.7を使用しています。
以下 インストール履歴 (遠回りもしているのでスムーズに入れたい場合は、1,2,6をやってください。
1.パッケージインストーラの実装(一般的なところなの読飛し可)
こちらのページを参考に setuptool と pip をインストールします。
setuptoolのインストール。こちらの「windows」の箇所から ez_setup.py をダウンロードし実行。
> python ez_setup.py
インストール後、環境変数PATH に「C:\Python27\Scripts」を追加します。
次にpipのインストール。
>easy_install pip
2.Visual C++ 2008 Express Edition のインストール
これをしないと「PIL」インストール時に、以下のエラーがでてインストール出来ません。
error: Setup script exited with error: Unable to find vcvarsall.bat
こちらhttp://go.microsoft.com/?linkid=7729279
からインストールして、インストーラーに従いインストール。
3.PILインスト-ル
pip install PIL
なんかエラー出た。
File "C:\Python27\lib\distutils\msvc9compiler.py", line 295, in query_vcvarsall raise ValueError(str(list(result.keys()))) ValueError: [u'path'] ---------------------------------------- Cleaning up... Command C:\Python27\python.exe -c "import setuptools;__file__='c:\\users\\user~1\\appdata\\local\\ temp\\pip_build_user\\PIL\\setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n') , __file__, 'exec'))" install --record c:\users\user~1\appdata\local\temp\pip-511z9e-record\instal l-record.txt --single-version-externally-managed failed with error code 1 in c:\users\user~1\appda ta\local\temp\pip_build_user\PIL Storing complete log in C:\Users\user\pip\pip.log
ぐぐったら以下の2点の事例あり。
1)Python for Windows extensions が入っていない問題
Visual Studio Professional Editionを入れろと言っているが、Python for Windows extensionsで補えるようなのでトライ。こちらの「Python for Windows extensions」とやらの「pywin32-214.win-amd64-py2.7.exe」をダウンロードします。
==> これやっったけど解決せず。
2) Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1 が入っていないため問題
Express Editionでは64bitコンパラが入っていなため、これをインストールする必要ありとのこと。
ここから、ダウンロードしてインストール。その後、以下の vcvars64.bat をコピー。
[コピー元] C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\vcvars64.bat [コピー先] C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64\vcvarsamd64.bat (ファイル名変更)
==> これで解決しました。
以下、実行結果。
-------------------------------------------------------------------- PIL 1.1.7 SETUP SUMMARY -------------------------------------------------------------------- version 1.1.7 platform win32 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] -------------------------------------------------------------------- *** TKINTER support not available (Tcl/Tk 8.5 libraries needed) *** JPEG support not available *** ZLIB (PNG/ZIP) support not 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. Successfully installed PIL Cleaning up...
4.テスト
このサイトにあるひと通り動かしてみる。がエラーが出て、画像が出ない。ここを見るとwindowsの場合はPILのインストーラを使えとある。PILを一回アンインストールしよう。
>>>from PIL import Image >>>image = Image.open('test.png') >>>print image.__class__ PIL.PngImagePlugin.PngImageFile >>>image.show() >>> image.show() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\PIL\Image.py", line 1483, in show _show(self, title=title, command=command) File "C:\Python27\lib\site-packages\PIL\Image.py", line 2123, in _show apply(_showxv, (image,), options) File "C:\Python27\lib\site-packages\PIL\Image.py", line 2127, in _showxv apply(ImageShow.show, (image, title), options) File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 41, in show if viewer.show(image, title=title, **options): File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 64, in show image = image.convert(base) File "C:\Python27\lib\site-packages\PIL\Image.py", line 679, in convert self.load() File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 189, in load d = Image._getdecoder(self.mode, d, a, self.decoderconfig) File "C:\Python27\lib\site-packages\PIL\Image.py", line 385, in _getdecoder raise IOError("decoder %s not available" % decoder_name) IOError: decoder zip not available
PILのアンインストール
pip uninstall PIL
5.PILの再インストール
ここから、インストーラ「Python Imaging Library 1.1.7 for Python 2.7 (Windows only)」をダウンロード。
ダブルクリックでインストール。
また問題起きた。「numpy python 2.7 is required which was not found in the registry」と表示されインストール出来ない。
ここを見ると、インストーラのバグのよう。
レジストリに以下を追加
・HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath
その後、本来のパスのHKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPathで設定されている値”C:\Python27\"を設定。
そして、インストーラーをもう一度実行。うまく行った。
さて、もう一回 PILで Image.show()テストしてみたがエラーになった。
>>>Imgage.show() ImportError: The _imaging C module is not installed
こちらを見ると、x64によるための問題のよう。
元リンクではx64ように別途作成されたインストーラを使う、またはx64ようにコンパイルし直すなど指示があった。
ということでアンインストール。
>pip uninstall PIL Uninstalling PIL: c:\python27\lib\site-packages\pil\pil-1.1.7-py2.7.egg-info Proceed (y/n)? y Successfully uninstalled PIL
6.PILをUnofficial Windows Binariesのサイトからダウンロードしてインストール
こちらからをPillow-2.2.2.win-amd64-py2.7.exe(PILと同等のよう)ダウンロードしてインストール。
その後もう一度画像表示テスト。うまく行った。
>>> from PIL import Image >>> image = Image.open('test.png') >>> print image.__class__ PIL.PngImagePlugin.PngImageFile >>> image.show() >>>
そういえばなんでPIL入れようとしたんだっけ、、、と忘れる始末。
さて画像処理やってみるか。次に画像解析やってみよっと。
[参照]
ライブラリ:PIL
PILハンドブック
[http://www.geocities.jp/showa_yojyo/note/python-pil.html#id12:title=Python Imaging Library (PIL) 利用ノート]