Installing and using PIL

Overcoming the sometimes tricky installation of the Python Imaging Library and improving image quality.

While some people have reporting installing PIL on MacOSX without problems, there are legion reports that say otherwise. Frequently this revolve around PIL installing without JPEG support. There are a myriad of solutions (involving editing make files, configure flags, using fink to install dependencies, using package managers, casting goat entrails, etcetera, etcetera ...), all of which I tried and had no joy with. The solution that finally worked was to grab the libjpeg package from here and install that. Afterwards the PIL install proceded without problems and tested fine.

While the image manipulations within the library are impressive, in some case the resultant image quality isn't quite good enough. To see one problem, create an image with a series of lines radiating out at various angles. Some of the lines will come out quite jagged and "pixelly". Text also shows some of these defects. One solution is to draw your picture at a much larger size, and then shrink it down using the anti-alias filter.

Another - and better - way is to use the aggdraw library for drawing PIL images. It uses internally the antigrain geometry library for high quality anti-aliased images. (In plain langauge - it produces slight fuzzy but smoother images.) One catch is that for drawing text, aggdraw uses the freetype text library. If you're on Windows, you're in luck - aggdraw installer includes freetype. If you're on a Unix system, you have to download and install freetype yourself. Follow the instructions, make sure you're installing freetype v2 not v1 and you shouldn't have anyproblems. What may trip you up is getting aggdraw to recognise that freetype is installed and where. In the setup.py file of your aggdraw distribution, you'll find a line:

FREETYPE_ROOT = "../../kits/freetype2011preblahblah"

The instructions say to edit this as necessary, but it's not clear to what. In fact, FREETYPE_ROOT should point at the folder that holds the include directory that holds the freetype2 folder. On most system this will be:

FREETYPE_ROOT = "/usr/local"

Unfortunately, if you try and install aggdraw before installing freetype, it seems to have trouble later recognising if the freetype library has appeared. (At least I did.) Presumably the setup caches some results or code from the first time it is is run, that never get updated. The easiest solution is just to unpack and use a fresh copy of aggdraw.

So you should install freetype to use aggdraw to use PIL. Simple.

Addendum July 2007

More installs of PIL, more surprises. This time my extensive install notes failed me in new and curious ways. While the Darwin ports installer has been useful in the past, this time it created a seemingly correct but non-functional copy of freetype. PIL would then bomb out during the compile. trying to install PIL via port ended up installing a whole new copy of Python.

Eventually, the instructions on the Python wiki on how to compile and PIL (via compiling and installing freetype and libjpeg) proved to work. Some notes:

  • You may need to delete to remove any dysfunctional freetype, libjpeg etc before following the wiki install, or else PIL might keep finding the bad libraries.
  • Some of the lines they have you delete in various make files don't appear exactly like they do in the notes, but it works anyway.
  • Finally, the install set of PIL may complain about a missing directory for man files - you can either ignore this or create the directories yourself.

As regards aggdraw, PythonMac now has pre-compiled packages for it, which is a huge time saver.

Addendum December 2010

Y'know, maybe you should just use Pillow instead, a 'friendly' re-implementation of PIL.