81

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")

17 Answers 17

86

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.

9
  • There is an error in the "aptitude install aptitude install" ;)
    – anders
    Jun 14, 2012 at 11:46
  • 12
    This doesn't work for me (Ubuntu 12.04, pip 1.1.7, Python 2.7). I uninstalled pip, apitude installed libfreetype6-dev, then ran sudo pip install --upgrade pil, but the problem persists. Dec 12, 2012 at 0:41
  • Didn't work for me either (on Mac). I installed libfreetype from source (from here), then ran sudo pip install --upgrade pil, but was told that PIL was up to date. Quite python and reran the original script, got the same error.
    – scubbo
    Feb 4, 2013 at 5:05
  • 2
    This worked for me on Xubuntu. sudo apt-get install libfreetype6-dev. And then in my virtualenv i ran, pip install Pillow (without sudo). You don't want to sudo pip in virtual environments.
    – pymarco
    Dec 4, 2013 at 16:47
  • 1
    For the reference of anyone still getting this issue, @Rafay's solution on using pip install --no-cache-dir pillow might help. May 17, 2017 at 21:52
57

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/

6
  • 1
    If you want linux binaries you'll have to get it from your distro's software repository I guess.
    – Imran
    Oct 25, 2010 at 3:40
  • This was exactly what I needed to get TTF support on Windows. The PIL binaries on the official page were having sxs issues when loading _imagingft.pyd. Thanks! Oct 25, 2010 at 5:49
  • Nowadays you can also get Pillow from PyPI - the Python Package Index.
    – martineau
    Apr 18, 2013 at 18:25
  • Looks like there is no PIL (not Pillow) any more. Is there any other source to download it?
    – LA_
    Sep 21, 2013 at 16:30
  • 2
    @LA_ Pillow is a drop-in replacement for PIL
    – Imran
    Sep 22, 2013 at 5:42
55

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
2
  • 1
    Thanks for the cache! Also, we can remove pip cache before re install Pillow: rm -rf ~/.cache/pip
    – LennyLip
    Jan 19, 2016 at 8:25
  • 3
    Just a comment, in CentOS 6: yum install freetype-devel libjpeg-devel libpng-devel
    – azuax
    Jun 22, 2016 at 13:21
17

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
1
  • use the first command to install *devel and then install python-imaging using "yum" (in epel repository) to get image displayed.
    – fanchyna
    Oct 23, 2014 at 19:17
14

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
6
  • 1
    Didn't work for me - after each line ln -s ... I got ln: /usr/local/lib/libfreetype.dylib: File exists. Problem persists. Do you have any more ideas?
    – scubbo
    Feb 4, 2013 at 5:14
  • Would you try ln -sf? f overwrites the existing files, so make sure to backup the existing files. Feb 4, 2013 at 10:29
  • Just tried ln -sf, as recommended. After the first such line, I got ln: /usr/local/include//freetype2: Operation not permitted. Repeating the operation with sudo gave the same error.
    – scubbo
    Feb 4, 2013 at 18:13
  • 1
    @volvox, try installing freetype with brew install freetype. Jul 29, 2014 at 10:46
  • 1
    @suzanshakya thanks that worked perfectly (once I'd installed homebrew).
    – volvox
    Jul 29, 2014 at 15:57
12

Worked for Ubuntu 12.10:

sudo pip uninstall PIL
sudo apt-get install libfreetype6-dev
sudo apt-get install python-imaging
2
  • 2
    I changed the third line from "sudo apt-get install python-imaging" to "pip install PIL" and it worked.
    – zephyr
    Jul 8, 2013 at 23:55
  • 1
    Worked for me on debian, note that I had libjpeg-dev zlib1g-dev libpng12-dev already installed
    – cgl
    Dec 26, 2014 at 23:13
12

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
4
  • If it doesn't work, add brew link freetype before install PIL.
    – user805627
    Oct 9, 2012 at 21:37
  • 1
    also the package is called freetype for macports users.
    – DanH
    Feb 21, 2013 at 6:02
  • 1
    I had to reinstall freetype and libjpeg through brew and then relink them using brew link --overwrite freetype and brew link --overwrite libjpeg, then reinstalled pil, then it finally worked. Hope this helps anyone
    – gitaarik
    Jun 20, 2013 at 14:23
  • brew install pil return error: ImportError: The _imagingft C module is not installed Apr 26, 2016 at 13:40
2

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

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

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

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

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
  • the second option worked for me. Thanks @tc_geophysics
    – MegaBytes
    Jan 20, 2016 at 6:14
1

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
  • won't sudo pip affect the global pil rather than the virtual environment pil? May 1, 2018 at 13:38
1

Instead of running: pip install Pillow

Run: pip install Image

darwin Big Sur pyenv

2
  • what does "darwin Big Sur pyenv" mean?
    – Ruli
    Dec 8, 2020 at 20:51
  • @Ruli it means he's using MacOS (darwin), the version is Big Sur and his python setup is via pyenv.
    – Ryu Kent
    Feb 11, 2021 at 12:06
1

In Windows 11 we need to solve this problem 'pip install --upgrade pip' 'pip install --upgrade Pillow'

pip install --upgrade Pillow

1
  • 2
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 27, 2022 at 11:12
0

Installing libtruetype-dev did not work for me on ubuntu container. My pillow was not compiled and pip installed one without truetype. (maybe it was cached this way by docker container provider). What did work was installing pill ow via conda:

pip uninstall -y pillow    
conda install pillow
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.