Kill PIL – The Python Imaging Library Headache or Building PIL on Mac OS X

December 4th, 2008 · 6 Comments · Creative Coding, Information Visualization

PIL Clusters

Pythonware’s PIL, the Python Imaging Library, is an extension to Python enabling you to process and arrange images quickly. It supports the popular JPEG and PNG image formats and provides font rendering using the freetype2 library. Unfortunately, PIL is not included in the standard Python environment on Mac OS X 10.4/10.5, so you have to download and build it.

First of all, you might find that there is plenty of forum/blog posts, articles etc. on the web attempting to provide useful instructions on how to build and install PIL in the right fashion. However, I found that most of them are telling different things and so I didn’t really trust them. I tried a lot and basically all of them failed. I am going to explain now, why that was.

Following the README what you need in advance is: libjpeg (6b), libpng3 and freetype2. You can get these using fink or MacPorts. Since I don’t like my machine to re-compile all the stuff that is easily available in a binary form, I used fink.

After installing the required libraries via fink I tried to build PIL using the command as described in the README file:

$ python install

… and all I got was a lousy:

--- TKINTER support ok
--- JPEG support ok
--- ZLIB (PNG/ZIP) support ok
*** FREETYPE2 supported not available

It turned out that the file did for some reason not search the correct directories for the freetype2 library. Fixing this is quite easy: just set

FREETYPE_ROOT = "/sw/lib/freetype2/lib"

Note: This does only apply as long as you use fink. MacPorts directories differ!

I again built PIL using the install command. Everything seemed OK this time. So, as suggested by the installer script, I tried to run PIL’s self test now using


which would result in

Failure in example: _info("Images/lena.jpg"))
from line #24 of selftest.testimage
Exception raised:
Traceback (most recent call last):
  File "./", line 499, in _run_examples_inner
    exec compile(source, "", "single") in globs
  File "", line 1, in 
  File "./", line 22, in _info
  File "PIL/", line 180, in load
    d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
  File "PIL/", line 375, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available
1 items had failures:
   1 of  57 in selftest.testimage
***Test Failed*** 1 failures.
*** 1 tests of 57 failed.

I googled and googled and found a lot of similar results, but no answers unfortunately. After some time I realized that PIL would run from any other directory, but only the failed. The reason for this is that in the Imaging-1.1.6 directory there is a PIL.pth file and a PIL subdirectory. Python picks these up and tries to run PIL from there. Of course this cannot succeed, since the library is not built properly for our system there. The solution is to delete both the PIL subdirectory and the PIL.pth file and there it goes – the self test runs through without any failures.

Hope I have saved you some valuable time.

Tags: ···

6 responses so far ↓

  • 1 desertrat  Feb 18, 2009 at 9:21 pm

    I wish I’d found your post 3 days ago. Would have kept that head-shaped dent out of my desk.

  • 2 Roland Frédéric  Feb 23, 2009 at 1:50 am

    Thank you for this post, it helped me a little bit toward installing PIL on Mac OS X. Unfortunatelly I was never able to complete Freetype2 installation using fink. I had to install it from source.

  • 3 Thanker  Jul 25, 2009 at 2:51 am

    exactly. Delete the PIL folder in the src folder is great 🙂 thumbs up…

  • 4 Phil  Aug 6, 2009 at 9:47 pm

    thank you for this!

  • 5 Bruce  Oct 7, 2009 at 8:58 pm

    I love you cuz I was getting really pissed at this.

  • 6 Martine Slimko  May 27, 2011 at 6:50 pm

    I would like to express my thanks to the writer just for rescuing me from such a dilemma. After surfing throughout the the net and finding ideas which are not helpful, I thought my entire life was over. Existing without the presence of approaches to the difficulties you have solved all through this short post is a critical case, and ones which might have in a negative way affected my entire career if I hadn’t come across your blog post. Your own competence and kindness in maneuvering the whole thing was excellent. I don’t know what I would have done if I had not encountered such a step like this. I am able to at this moment relish my future. Thanks for your time so much for your impressive and results-oriented guide. I will not think twice to endorse your blog post to any individual who needs care about this problem.

© 2013 Tobis Lensing. All rights reserved. Powered by Wordpress — based upon Cutline by Chris Pearson. This page reflects the personal opinion of the author and is in no way linked to institutions the author is working or has worked for. For more information, see the disclaimer and Privacy Policy.