5 Installing PIL on Mac OS X Snow Leopard for use in Google App Engine

Jan 27th, 2010   2:42 am

Python users who use Django or Google App Engine on the Mac, will likely benefit from the python imaging library (PIL).

However, installing on Mac OS X (Snow Leopard) can be tricky. There are many blog posts on the issue. The most common problem is that PIL and libjpeg (a dependency) seem to install OK, but when you go to use it in your Python code, you get “_jpeg_resync_to_restart” errors.

A further complication is the fact that Google App Engine uses Python version 2.5 (and not the default 2.6 that comes with Snow Leopard). If you just need PIL for Django, you can ignore the 2.5 references (i.e. python2.5 becomes python).

Here’s how I got it working on my machine (the steps are a combination of others’ blog posts, but main credit goes to Michael Richardson‘s post):

  1. Download the latest libjpeg .tar.gz (version v7 at time of writing): http://www.ijg.org/files/.
  2. Download the latest PIL version for Python 2.5 (v1.17 at time of writing): http://www.pythonware.com/products/pil/
  3. Extract libjpeg and do the standard install affair:
    export CC="gcc -arch i386"
    sudo make install

    from within the directory. The first line is the gem that many other blog posts leave out (and it assumes you’re using BASH)

  4. Extract the PIL and change in to that directory.
  5. [optional] libjpeg will have been installed in /usr/local/lib/, by default, above. Normally this directory will be picked up automatically by the PIL install below, but just for good measure (or if you have problems later in the PIL install), edit the setup.py, look for the line
    JPEG_ROOT = None

    and change that line to

    JPEG_ROOT=libinclude( "/usr/local" )

    and save it.

  6. Install PIL by running
    sudo python2.5 setup.py install

That was enough to get it running for me. I can start App Engine (I use the Google-provided launcher), and use the Image API.


Mar 16th, 2010   8:08 pm

Ran into this 2 days ago, this post would've been very helpful then :) You might want to add the version nr of libjpeg in your post because this was the issue for me; v7, or 0.7, of libjpeg introduces that '_jpeg_resync_to_restart' and PIL doesn't know how to handle it. If you simply use 0.6 of libjpeg everything's fine (at least for me it was). But I'm not sure if that version is still distributed on that site, but anyway.
Mar 28th, 2010   9:58 pm

You might also find this useful: http://blog.mfabrik.com/2009/11/19/installing-python-imaging-library-pil-under-virtualenv-or-buildout/


Mar 28th, 2010   10:13 pm

Thijs, thanks for the tip, I've amended the version in the post. It was version 7, but I've not noticed the resync error you mentioned. I've only used basic features of PIL (cropping & resizing), so I'm not sure if that explains it. Mikko, thanks for the link.
May 27th, 2010   7:02 pm

\o/ Tks a lot dude... simple and worked like a charm! hugs
Vaclav Jan
Feb 8th, 2011   11:51 am

Thank you very much, much, much :-) , worked excellent , Once again thank you from Prague, Czech Republic

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>