読者です 読者をやめる 読者になる 読者になる

[[ともっくす alloc] init]

ともっくすの雑多な日記と技術的なメモ

Python Imaging Library (PIL)で画像を扱いたいし文字も使いたいしフォントを指定したい

Pythonで画像を扱おうと思ったら,Python Imaging Library(通称:PIL)らしいので,インストールしてみる.

$ pip install PIL
(略)
--------------------------------------------------------------------

PIL 1.1.7 SETUP SUMMARY

--------------------------------------------------------------------

version       1.1.7

platform      linux2 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)

              [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]

--------------------------------------------------------------------

*** TKINTER support not available

*** JPEG support not available

--- ZLIB (PNG/ZIP) support available

*** FREETYPE2 support not available

*** LITTLECMS support not available

--------------------------------------------------------------------
(略)

「ほげほげ support not available」とかいう文字が見えるが気にしない.


とりあえず,何か文字を書いてみようと.まず,フォントを指定する.*1

$ python
>>> import ImageFont
>>> ImageFont.truetype("〜〜.ttf", 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/〜〜/site-packages/PIL/ImageFont.py", line 218, in truetype
    return FreeTypeFont(filename, size, index, encoding)
  File "/〜〜/site-packages/PIL/ImageFont.py", line 134, in __init__
    self.font = core.getfont(file, size, index, encoding)
  File "/〜〜/site-packages/PIL/ImageFont.py", line 34, in __getattr__
    raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed

ImageFont.truetype(フォントのパス, サイズ)でフォントを指定できるわけだが,ImportErrorが.

インストールしたときの,「FREETYPE2 support not available」が原因.

というわけで,ここを参考にさせていただき,PILを一旦アンインストールした後に,freetype2の関連ライブラリのシンボリックリンクを張って,もっかいインストール.

$ pip uninstall PIL
$ ln -s /usr/X11/include/freetype2 /usr/local/include/
$ ln -s /usr/X11/include/ft2build.h /usr/local/include/
$ ln -s /usr/X11/lib/libfreetype.6.dylib /usr/local/lib/
$ ln -s /usr/X11/lib/libfreetype.6.dylib /usr/local/lib/libfreetype.dylib
$ pip install PIL

これでImportErrorも出ない.

無事解決!


この3つに関しては,機会があれば.
→「TKINTER support not available」「JPEG support not available」「LITTLECMS support not available」


PILのREADMEに,

1. Prerequisites.

   If you need any of the features described below, make sure you
   have the necessary libraries before building PIL.

   feature              library
   -----------------------------------------------------------------
   JPEG support         libjpeg (6a or 6b)

                        http://www.ijg.org
                        http://www.ijg.org/files/jpegsrc.v6b.tar.gz
                        ftp://ftp.uu.net/graphics/jpeg/

   PNG support          zlib (1.2.3 or later is recommended)

                        http://www.gzip.org/zlib/

   OpenType/TrueType    freetype2 (2.3.9 or later is recommended)
   support              
                        http://www.freetype.org
                        http://freetype.sourceforge.net  

   CMS support          littleCMS (1.1.5 or later is recommended)
   support              
                        http://www.littlecms.com/

   If you have a recent Linux version, the libraries provided with the
   operating system usually work just fine.  If some library is
   missing, installing a prebuilt version (jpeg-devel, zlib-devel,
   etc) is usually easier than building from source.  For example, for
   Ubuntu 9.10 (karmic), you can install the following libraries:

       sudo apt-get install libjpeg62-dev
       sudo apt-get install zlib1g-dev
       sudo apt-get install libfreetype6-dev
       sudo apt-get install liblcms1-dev

   If you're using Mac OS X, you can use the 'fink' tool to install
   missing libraries (also see the Mac OS X section below).

   Similar tools are available for many other platforms.
(略)
--------------------------------------------------------------------
Additional notes for Mac OS X
--------------------------------------------------------------------

On Mac OS X you will usually install additional software such as
libjpeg or freetype with the "fink" tool, and then it ends up in
"/sw".  If you have installed the libraries elsewhere, you may have
to tweak the "setup.py" file before building.

って,書いてあるんだが,まあ,いっか.

*1:こうやっていきなり文字を書こうとするあたりが恣意的すぎる