74
votes

I've tried lots of solution that posted on the net, they don't work.

>>> import _imaging
>>> _imaging.__file__
'C:\\python26\\lib\\site-packages\\PIL\\_imaging.pyd'
>>>

So the system can find the _imaging but still can't use truetype font

from PIL import Image, ImageDraw, ImageFilter, ImageFont


im = Image.new('RGB', (300,300), 'white')
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('arial.ttf', 14)
draw.text((100,100), 'test text', font = font)

Raises this error:

ImportError: The _imagingft C module is not installed

File "D:\Python26\Lib\site-packages\PIL\ImageFont.py", line 34, in __getattr__
  raise ImportError("The _imagingft C module is not installed")
15

15 Answers

57
votes

Your installed PIL was compiled without libfreetype.

You can get precompiled installer of PIL (compiled with libfreetype) here (and many other precompiled Python C Modules):

http://www.lfd.uci.edu/~gohlke/pythonlibs/

82
votes

On Ubuntu, you need to have libfreetype-dev installed before compiling PIL.

i.e.

$ sudo apt-get install libfreetype6-dev
$ sudo -s
\# pip uninstall pil
\# pip install --no-cache-dir pil

PS! Running pip install as sudo will usually install packages to /usr/local/lib on most Ubuntu versions. You may consider to install Pil in a virtual environment (virtualenv or venv) in a path owned by the user instead.

You may also consider installing pillow instead of pil, which I believe is API compatible: https://python-pillow.org. Note that Pillow also requires libfreetype-dev and you might need to follow the same uninstall/install steps if libfreetype-dev was not present during the initial installation.

48
votes

The following worked for me on Ubuntu 14.04.1 64 bit:

sudo apt-get install libfreetype6-dev

Then, in the virtualenv:

pip uninstall pillow
pip install --no-cache-dir pillow
17
votes

solution for CentOS 6 (and probably other rpm based):

yum install freetype-devel libjpeg-devel libpng-devel

pip uninstall pil Pillow
pip install pil Pillow
14
votes

In OS X, I did this to solve the problem:

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
12
votes

Basically, you need to install freetype before installing PIL.

If you're using Homebrew on OS X it's just a matter of:

brew remove pil
brew install freetype
brew install pil
12
votes

Worked for Ubuntu 12.10:

sudo pip uninstall PIL
sudo apt-get install libfreetype6-dev
sudo apt-get install python-imaging
2
votes

For OS X (I'm running 10.6 but should work for others) I was able to get around this error using the advice from this post. Basically you need to install a couple of the dependencies then reinstall PIL.

2
votes

For me none of the solutions posted here so far has worked. I found another solution here: http://codeinthehole.com/writing/how-to-install-pil-on-64-bit-ubuntu-1204/

First install the dev packages:

$ sudo apt-get install python-dev libjpeg-dev libfreetype6-dev zlib1g-dev

Then create some symlinks:

$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/

Afterwards PIL should compile just fine:

$ pip install PIL --upgrade
2
votes

The followed works on ubuntu 12.04:

pip uninstall PIL
apt-get install libjpeg-dev
apt-get install libfreetype6-dev
apt-get install zlib1g-dev
apt-get install libpng12-dev
pip install PIL --upgrade

when your see "-- JPEG support avaliable" that means it works.

But, if it still doesn't work when your edit your jpeg image, check the python path!!
My python path missed '/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/', so I edit the ~/.bashrc add the following code to this file:

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/

then, finally, it works!!

1
votes

Ubuntu 11.10 installs zlib and freetype2 libraries following the multi-arch spec (e.g. /usr/lib/i386-linux-gnu). You may use PIL setup environment variables so it can find them. However it only works on PIL versions beyond the pil-117 tag.

export PIL_SETUP_ZLIB_ROOT=/usr/lib/i386-linux-gnu
export PIL_SETUP_FREETYPE_ROOT=/usr/lib/i386-linux-gnu
pip install -U PIL

Since your multi-arch path may be different (x86-64), it's preferable to install the -dev packages and use pkg-config to retrieve the correct path.

pkg-config --variable=libdir zlib
pkg-config --variable=libdir freetype2

Another way given by Barry on Pillow's setup.py is to use dpkg-architecture -qDEB_HOST_MULTIARCH to obtain the proper library directory suffix.

See https://bitbucket.org/effbot/pil-2009-raclette/issue/18

1
votes

I used homebrew to install freetype and I have the following in /usr/local/lib:

libfreetype.6.dylib libfreetype.a libfreetype.dylib

But the usual:

pip install pil

Does not work for me, so I used:

pip install http://effbot.org/downloads/Imaging-1.1.6.tar.gz

1
votes

In my Mac, the following steps in terminal works:

$ brew install freetype
$ sudo pip uninstall pil
$ sudo pip install pillow

hopes it works for you. Good luck!

1
votes

Instead of running: pip install Pillow

Run: pip install Image

darwin Big Sur pyenv

-1
votes

【solved】
In my ubuntu12.04, after I installed python-imaging using apt-get, it works.