« PHP Frameworks: So many options, few are sufficient Blog Engine moved to Movable Type, Feed Address Change »

April 1, 2009

By Reza Muhammad

7 Comments,

Setting up PIL with libjpeg on Mac OS X Leopard

Background

For the past few months I've been playing around with Django, and it has come to a situation where I need Imaging Library to fulfill my project's requirements. In Python, it is handled with PIL. So, here's what I did to setup PIL with libjpeg support on Mac OS X Leopard.

Default vanilla Mac OS X install doesn't have libjpeg by default, so I need to install them first.

Methods of Installing PIL on Leopard

There are a few methods you can use to install PIL on Mac OS X, they are:

  1. Install libjpeg from source, and then also install PIL from source
  2. Use MacPorts / fink to install libjpeg and PIL automatically
  3. Or, you can also install libjpeg from MacPorts, and then PIL from source

I decided to use the third approach. First of all, I'm still not confident to install library from source, and also I want to use binary form where as much as possible. Also, I didn't install everything from MacPorts even though they are available. My primary reason is, if I installed PIL from MacPorts, it will also install Python, which I don't want. Apple already included Python in the default install. If you prefer to use fink over MacPorts, you can also do this

Installation

  1. Download MacPorts and run the installer
  2. Install libjpeg by using these command lines: sudo port install jpeg. This will install libjpeg in "/opt/local/lib"
  3. Download PIL from source from pythonware
  4. Extract the file:

    $ tar zxvf Imaging-1.1.6.tar.gz
    $ cd Imaging-1.1.6
    
  5. Edit setup.py, and change the line JPEG_ROOT to: JPEG_ROOT = "/opt/local/lib/libjpeg.dylib"

  6. Compile and build the source: sudo python setup.py install

The above step will install PIL package in "/Library/Python/2.5/site-packages/". This is the default directory where Python packages are located in Mac OS X.

Verify the Package

Now, here's what I did to check whether PIL was successfully installed on my computer.

In Python interpreter, I did the following:

% python
Python 2.5.1 (r251:54863, Nov 12 2008, 17:08:51)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more
>>> import Image
>>> image = Image.open("/Users/rezmuh/test-image.jpg")
>>> image.save("/Users/rezmuh/pil-image.jpg")
>>> ^D

If there is no error and you can see the new image copied from an old image, then congratulations, PIL is successfully installed :)

If you know any simpler ways to install PIL, please let me know

7 Comments

did exactly as you said.

but got this error:

--------------------------------------------------------------------
PIL 1.1.6 BUILD SUMMARY
--------------------------------------------------------------------
version 1.1.6
platform darwin 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)]
--------------------------------------------------------------------
--- TKINTER support ok
--- JPEG support ok
--- ZLIB (PNG/ZIP) support ok
--- FREETYPE2 support ok
--------------------------------------------------------------------
To check the build, run the selftest.py script.
macbook-pros-macbook-pro:Imaging-1.1.6 macbookpro$ python selftest.py
*****************************************************************
Failure in example: _info(Image.open("Images/lena.jpg"))
from line #24 of selftest.testimage
Exception raised:
Traceback (most recent call last):
File "./doctest.py", line 499, in _run_examples_inner
exec compile(source, "", "single") in globs
File "", line 1, in
File "./selftest.py", line 22, in _info
im.load()
File "PIL/ImageFile.py", line 180, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "PIL/Image.py", line 375, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

Hi Alex,
I'm sorry for the late reply.

It looks like you're getting errors in selftest.py, which I haven't tried yet.

But, if you run Python Interpreter, can you do "import Image" ?

I usually don’t post in Blogs but your blog forced me to, amazing work.. beautiful …

Thanks William :)

While I don't think I deserve full credits of what I do but it is nice to see there's someone who enjoys reading it.

Hope I will be more consistent on updating the contents. :P

I tried it and I still get the same "Caught an exception while rendering: encoder jpeg not available" despite now when I build PIL I get "--- JPEG support ok". Is there any way to test that the JPEG_ROOT path has been set correctly during the installation? I'm asking because mine was a re-installation.

christian: If you want to make sure your JPEG_ROOT is set correctly, you can look at setup.py in your Imaging source directory. This is what I have in my setup.py:

JPEG_ROOT = "/opt/local/lib/libjpeg.dylib"

You also need to make sure that /opt/local/lib/libjpeg.dylib exists.

Nice blog! Very interesting aspects. I will often read it. May i post your site link on my site?

Scroll to Top