the blog of

Python 2.7, libjpeg, PIL on OSX

March 6, 2011

It took me entirely too long to get the right set of steps together for installing libjpeg and PIL on OSX. It seemed worthwhile to write down.

The web is littered with people having the "jpeg_resync_to_restart" error, accompanied by a zillion walkthroughs of how to get libjpeg and PIL configured and installed on OSX. If your experience is anything like mine, though, none of those walkthrough seem to work.

So, here's another walkthrough, outlining what worked for me. Hopefully, it helps you too.

Delete all the old crap

You've probably been trying to install libjpeg and PIL for a few hours now, and there are likely remnants of various versions strewn throughout your computer. The first thing to do is get rid of all of that. Here's what I've found you'll need to remove.

cd /usr/local/include
sudo rm jpeglib.h
cd /usr/local/lib
sudo rm libjpeg*.*
cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
sudo rm -R ./PIL
sudo rm PIL.pth

Notice that I am doing this in Python2.7 above. You'll need to change your directories a bit if you are doing this for some other version of python.

Update (3/20/2011):

I was trying to get PIL and libjpeg setup on a separate computer recently and I found that if you have installed PIL or libjpeg in the past (for previous versions of python) using MacPorts or something similar, you'll like have some libjpeg remnants scattered in some places other than those listed above.

I found that most of mine were sitting inside /opt/local/include and /opt/local/lib. However, to be more thorough, run this command:

find / -name *jpeg* -print

That will show you anything that even remotely smells of libjpeg. You can then delete selectively. 

Furthermore, I cleaned up my ~/.bash_profile to only include things on my PATH that I knew I wanted.

libjpeg

At this time of this post, the most recent version of libjpeg was 8c. Get it. Make sure you get the .tar.gz, the zip seems to be configured for windows. Once you have that, extract it and go into that directory.

Then, follow these instructions exactly.

export CC="gcc -arch i386"
./configure
make
sudo make install

Note: do not 'sudo' the ./configure OR the first make. That will blow it.

PIL

At the time of this post, the most recent version of PIL was 1.1.7. Get it, extract it, go into that directory.

Configure Setup.py

Open up 'setup.py', find the line that reads:

JPEG_ROOT = None

...and change it to:

JPEG_ROOT = libinclude("/usr/local")

From what I've gathered, that last step is actually no longer necessary. I've now done a couple installs of PIL without changing the JPEG_ROOT. If you comb through the source code, you'll see that the script will automatically look in /usr/local/lib, so adding it isn't really needed.

Install PIL

Save that file, head into the terminal in that directory and run the setup.

sudo python2.7 setup.py install

Notice that I am explicitly using the 'python2.7' instead of just 'python'. I doubt this really makes a difference, considering my 'python' is just a symlink to 'python2.7'. However, the iteration through all of this that it finally worked, I used python2.7 to run the setup and I'm a bit superstitious.

See if it works

To see if it all worked... run this command:

otool -L /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/_imaging.so

That should spit out a bunch of lines, one of which will reference your libjpeg. It will look something like this:

/usr/local/lib/libjpeg.8.dylib (compatibility version 12.0.0, current version 12.0.0)

If you get that line back, then chances are it all worked. If you don't see a reference to your libjpeg, something went wrong with the libjpeg configuration/installation.

To test it more definitively, open up a terminal and run the python command line.

python
import _imaging

No news is good news, the import shouldn't kick back any errors.

Hope this worked for you, good luck.

blog comments powered by Disqus