that was difficult — installing the Python Imaging Library (PIL)

This post is a little dry, so I’ll start it off with a graphic — — of the number of visitors to this blog since October 8 (I won’t get into scale here). The preceding graphic is a sparkline produced using Joe Gregorio’s fabulously simple code. The other interesting aspect of the sparkline is that it is contained in an inline “data: URI”, meaning that it is not an external image, but rather is encoded directly into the html code (view source of this page to see what I’m talking about). There are many advantages to this scheme, but I won’t go into them here.

If you don’t see the preceding sparkline, then you’re probably using a crappy browser. You probably also can’t see my embedded Flash movies in other posts. Switch to Firefox or Safari. I believe in accessibility and whatnot, but this is a blog about cartography, graphics, and programming, so I have a few expectations for my audience’s internet capabilities.

Onto the main event. Joe Gregorio’s sparkline code requires the Python Imaging Library, or PIL. Python lacks a standard imaging library, but PIL is the de facto standard. Unfortunately, this means you must install it yourself. Easier said than done. At least, it was for me. If you’re very comfortable with Apple’s Terminal and UNIX commands then this should be no biggie. But alas I was not. I was used to DMGs and auto-installers. And it took me a *long time* to get this going. So below I’ll walk you through how I finally got the library installed. I should mention that this is on a Mac running Leopard and MacPython 2.5. I guess I can’t speak for other configurations, but the tips below should still hold true.

Before installing PIL you’ll need to have Apple’s XCode installed on your machine. I have no idea why, but it seems to be necessary. This is the easy part. Just go here and download the XCode 3.0 DMG (you’ll need a free ADC membership to download it). It’s a beefy download, but once you have it, just run the installer.

Assuming you have Python installed, you’ll need three more things to get this going: obviously PIL (latest tar.gz here), but also the JPEG library (latest tar.gz here) and Freetype2 (latest tar.gz here). Install libjpeg and freetype2 first. The most important thing to remember is to NOT extract these tar.gz files using the standard Archive Utility (don’t just double-click the tar.gz file in Finder). This will corrupt the files and lead to many errors later than you don’t understand (trust me here). Rather, open Terminal and navigate to the directory where you have your tar.gz files (I would just put them in your root directory — then you don’t have to navigate anywhere). Let’s install freetype2 first. Type the following in terminal (NOTE: you’ll be asked for your password a few times — this is just your standard system password):

tar zxvf freetype-2.3.5.tar.gz
cd freetype-2.3.5
./configure
make
make install

Hopefully that didn’t complain. If not, Freetype2 is installed!! Now go back to your root directory and we’ll install the JPEG Library:

tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure
make
sudo make install-lib

Now we’re really having fun. The JPEG library should be installed. Now for PIL itself. Go back to where your tar.gz files are kept and type the following:

tar zxvf Imaging-1.1.6.tar.gz
cd Imaging-1.1.6
sudo python setup.py install

Now it’s installed and you’re ready to use it. However, the path to PIL won’t be in your standard Python sys.path, so you’ll need to add it before you can start rocking PIL. To do so, open IDLE or whatever you’re using for Python and type the following into the command line:

import sys
sys.path.append('/path/to/Imaging-1.1.6/PIL')
sys.path.append( '/path/to/Imaging-1.1.6/build/lib.macosx-10.5-i386-2.5')
import Image

In the above, ‘path to’ is the path to wherever you initially extracted the Imaging library. In my case it is simply ‘/Users/zachjohnson’. The second appended path is the path to the binary _imaging module which is also required by the PIL classes. The above path should work if you’re running Leopard. If it doesn’t, search your computer for ‘_imaging.so’ and use whatever path that file is found in. If ‘import Image’ in the above code didn’t complain, then you are off and running. Try running this file to generate the html for the sparkline shown above.

One Comment

  1. hot damn. thanks… many other not-so-friendly ways to do this, but yours rocked the hissle.

    ted
    Posted March 13, 2009 at 11:32 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *