Python Imaging Library on Leopard Intel
Installing anything that uses mod_python on Leopard Intel has become a major pain. The problem is that Apache under Leopard runs as 64-bit, but most things are compiling as 32-bit. I’ve had to use SQLite for the backend Django database on our Leopard Server because of this. There is no MySQL package for 64-bit Intel, and that makes it very difficult to compile Python’s MySQLdb as x86_64, which in turn causes Django to fail when run under Apache, because Apache can’t “find” the right code for the architecture.
One of the things I want that Django install to do is manage images, so I thought I’d try installing django-photologue. However, photologue requires the Python Image Library; and the Python Imaging Library requires libjpeg.
The big problem with tracking this problem down is that Django and the Python Image Library hide the real error, and instead say that “The _imaging C module is not installed” (triggered by .load() in /Library/Python/2.5/site-packages/PIL/Image.py). Django hides it behind “Upload a valid image. The file you uploaded was either not an image or a corrupted image.” I discovered the real errors by commenting out the "try except" around that code.
It took a bit of searching to find the various mailing list and blog entries that address these issues, so I’m combining them here for my own future reference and yours.
Compile libjpeg as x86_64
First, libjpeg; in jpeg-6b/Makefile add -arch x86_64 in two places in the Makefile:
- CFLAGS= -Os -I$(srcdir) -arch x86_64
- # Link-time cc options:
- LDFLAGS= -arch x86_64
then do “make all”.
Second, re-run GCC to create the dynamic library libjpeg.dylib.
- gcc -dynamiclib -flat_namespace -undefined suppress -o .libs/libjpeg.62.0.0.dylib jcapimin.lo jcapistd.lo jctrans.lo jcparam.lo jdatadst.lo jcinit.lo jcmaster.lo jcmarker.lo jcmainct.lo jcprepct.lo jccoefct.lo jccolor.lo jcsample.lo jchuff.lo jcphuff.lo jcdctmgr.lo jfdctfst.lo jfdctflt.lo jfdctint.lo jdapimin.lo jdapistd.lo jdtrans.lo jdatasrc.lo jdmaster.lo jdinput.lo jdmarker.lo jdhuff.lo jdphuff.lo jdmainct.lo jdcoefct.lo jdpostct.lo jddctmgr.lo jidctfst.lo jidctflt.lo jidctint.lo jidctred.lo jdsample.lo jdcolor.lo jquant1.lo jquant2.lo jdmerge.lo jcomapi.lo jutils.lo jerror.lo jmemmgr.lo jmemnobs.lo -lc -install_name /usr/local/lib/libjpeg.62.dylib -compatibility_version 63 -current_version 63.0 -arch x86_64
You can now check that the correct architecture (x86_64) is present and install it if so:
- file .libs/libjpeg.dylib
- sudo make install-lib
That gets the right architecture into libjpeg.dylib so that the Python Imaging Library can use it.
Compile Python Imaging Library as x86_64
In PIL (Imaging-1.1.6) remove the build files if you’ve already tried building the library (rm -r build/*). Then, edit setup.py:
[toggle code]
- OrigExtension = Extension
-
def Extension(*args, **kwargs):
- extra_args = ['-arch', 'x86_64']
- kwargs['extra_compile_args'] = extra_args + kwargs.get('extra_compile_args', [])
- kwargs['extra_link_args'] = extra_args + kwargs.get('extra_link_args', [])
- return OrigExtension(*args, **kwargs)
Now you can build and install the image library:
- python setup.py build
- sudo python setup.py install
Note that I’m pretty sure that other things, such as freetype, are still going to fail. If I have need to look at it, and successfully do so, I’ll add that to this article also.
- unknown load command in shared library
- Compiling libjpeg for Mac OS X x86_64.
- PIL not working in mod_python on macosx 10.5
- If your CPU is 64 bit then all the libraries and loadable modules must also be 64 bit capable as Apache will run as x86_64.
- django-photologue
- “Photologue replaces the ImageField in Django with a powerful system that supports resizing and image effects as well as providing a turn-key photo gallery solution. Photologue embraces the Django admin and smoothly integrates with photo thumbnails and effect previews.”
- Python Imaging Library
- “The Python Imaging Library (PIL) adds image processing capabilities to your Python interpreter. This library supports many file formats, and provides powerful image processing and graphics capabilities.”
- Independent JPEG Group
- “IJG is an informal group that writes and distributes a widely used free library for JPEG image compression.”
More Python
- Parsing JSKit/Echo XML comments files
- While I’m not a big fan of remote comment systems for privacy reasons, I was willing to use JSKit as a temporary solution because they provide an easy XML dump of posted comments. This weekend, I finally moved my main blog to custom comments; here’s how I parsed JSKit’s XML file.
- Put a relative clock on your Desktop with GeekTool
- There are a lot of desktop clocks that show the absolute time. But sometimes you just want to know if the time is today, or yesterday, or two days ago. Here’s how to do it with Python and GeekTool.
- Multiple tables on the same command
- The way the “random” script currently stands, it does one table at a time. Often, however, you have more than one table you know you’re going to need. Why not use one command to rule them all?
- Easier random tables
- Rather than having to type --table and --count, why not just type the table name and an optional count number?
- Programming for Gamers: Choosing a random item
- If you can understand a roleplaying game’s rules, you can understand programming. Programming is a lot easier.
- 24 more pages with the topic Python, and other related pages
