April 2009 Archives
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:
- Install libjpeg from source, and then also install PIL from source
- Use MacPorts / fink to install libjpeg and PIL automatically
- 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
- Download MacPorts and run the installer
- Install libjpeg by using these command lines:
sudo port install jpeg. This will install libjpeg in "/opt/local/lib" - Download PIL from source from pythonware
Extract the file:
$ tar zxvf Imaging-1.1.6.tar.gz $ cd Imaging-1.1.6Edit setup.py, and change the line JPEG_ROOT to:
JPEG_ROOT = "/opt/local/lib/libjpeg.dylib"- 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