How to install PIL with libjpeg on Mac OS X 10.5

Installing the Python Imaging Library on OS X is a snap. Installing it with libjpeg support is a bit trickier. Since you will probably want to work with JPEG images, here’s how I got PIL and libjpeg working nicely on OS X 10.5.

#1: Install fink

fink is a package manager for OS X, similar to what apt-get is for Debian. Normally I prefer to compile packages manually rather than install from a ported binary (at least on OS X), but in this case it’s much easier to just install the binary. From what I understand, libjpeg needs to be patched before compiling on OS X which can add a bit of headache. A much simpler solution is to just bite the bullet and download fink.

#2: Install libjpeg

This one is easy: fink install libjpeg

#3: Download the PIL source code

Head over to the PIL downloads and grab the source tarball. Extract it anywhere in your filesystem. Mine lives at ~/code/python/Imaging-1.1.6/

#4: Tell PIL where to find libjpeg

This is the step nobody tells you about. For some reason you need to manually tell PIL‘s setup file where the libraries for libjpeg are loaded. It’s important you do this before installing PIL.

  1. First let’s find out where fink installed libjpeg. Type dpkg -L libjpeg. You’ll see a list of files. The file you want ends in *.dylib. For example, mine is /sw/lib/libjpeg.dylib
  2. Open ~/code/python/Imaging-1.1.6/setup.py and find the line that says:
    JPEG_ROOT = None
    Replace None with the path you just found, ie:
    JPEG_ROOT = ”/sw/lib/libjpeg.dylib”

#5: Install PIL

Now that PIL knows where to find the libjpeg libraries, you can install it and expect JPEG support. Change into the source code directory and install PIL like so:
sudo python setup.py install

#6: Try it out!

Here comes the moment of truth. Fire up your python shell and type:

>>> import Image
>>> img = Image.open(“some_image.jpg”)
>>> img.save(“some_image_copy.jpg”)
>>> img.show()

You should be able to perform those operations without getting any exceptions. Note that typing import Image and Image.open(“some_image.jpg”) isn’t enough to verify that libjpeg is working properly—you must actually try saving or showing a JPEG image instance to test the decoder.

Not nearly as elegant as apt-get install python-imaging, but hopefully it will save you some headache if you ever need to get PIL and libjpeg working!


Leave a comment

Comments

Me going grey

Kyle Fox is a developer and designer currently located in Edmonton, Alberta, Canada. (more)