views:

127

answers:

2

I am trying to get PIL working with Django 1.2.1 and Python 2.7 on Snow Leopard

I have followed instructions I found here on SO and I should be doing it right.

The imports and selftest.py works fine and I both save and open images in the interactive python, but Django cannot use it.

I get the error

The _imaging C module is not installed

Why on earth does PIL seem to work everywhere but Django? I just doesn't make any sense.

I have even tried reinstalling Django after installing libjpeg and PIL, but with no results, what am I doing wrong?

EDIT: I have just discovered something weird. I can open and save images just fine, by using the interactive python in terminal. But for some reason, when I save an image, the colors are inverted!

The code used is:

im = Image.open("/Users/Me/Downloads/9.jpg")
im.save("/Users/Me/Downloads/8.jpg")

Does that give any clues as to why it does not work in Django at all?

EDIT 2: Nevermind that last part, it seems that the jpg I chose, was with CMYK colors, and that cannot be saved directly as an RGB or something along those lines.

EDIT 3: And then again, maybe it is correct that Django is looking in the wrong place.

Exception Location: /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/PIL/Image.py in __getattr__, line 36
Python Executable:  /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.0

This is what Django puts out. I just looked at the version, silly me. The top line clearly states that it is looking in the 2.5 path. Wonder why its 2.5 since SL should be born with 2.6, oh well, no matter.

Can anyone then tell me how to direct Django to use the newer ones? The solution with changing manage.pydid nothing. Why is that, that should have told Django to use 2.7 no matter what.. right?

A: 

Make sure that your Django code is referencing the same version of Python that you're using "everywhere" else. Snow Leopard comes with Python 2.6.1 by default found at /usr/bin/python.

If you've installed Python 2.7 by some other means, it's probably found at another path with a symlink at /usr/bin/python2.7 pointing to its actual location. If PIL is installed under Python 2.7, then you cannot be referencing /usr/bin/python in your code because that is pointing you to the wrong version of Python.

The best practice would be to explicitly specify that you want to use Python 2.7 in the shebang (#!/usr/bin/env python2.7) for your Django initialization script (e.g. manage.py).

jathanism
Thanks for your input, but that should not be it since at the Django debug page it lists the python version to be 2.7.0 so it should using the correct python, and I have verified that PIL is installed under 2.7.Just for good measure I changed manage.py per your directions, but with no change in the result.In the installer for Python2.7 there is a script that should change the default python to the newly installed 2.7. From what I can see, it has done exactly that.
Mathias Nielsen
I found the error. I had an earlier install of PIL under Python2.5 and for some reason Django really wants to add the path to site-packages for 2.5 (not for 2.6) to its sys.path. This does not show up outside of Django and I have no idea why it chose the 2.5 folder to add, but never the less I just removed the PIL folder from 2.5 and used your tip on changing manage.py This did the trick and everything seems to be working fine now. Dunno if you should add this to your answer as a note since it seems unlikely that anyone else will have this error. Do as you wish :)
Mathias Nielsen
+1  A: 

Hi Mathias.

I wrote a pretty extensive tutorial on how to get PIL, libjpeg to work on Snow leopard. Maybe this will help you out.

http://appelfreelance.com/2010/06/libjpeg-pil-snow-leopard-python2-6-_jpeg_resync_to_restart/

If you don’t have this download it first.

http://www.ijg.org/files/jpegsrc.v7.tar.gz

go into your shell environment and untar by running the following

tar -zxvf jpegsrc.v7.tar.gz


cd jpeg-7

then run

sudo make clean
sudo CC="gcc -arch i386"./configure --enable-shared --enable-static
sudo make
sudo make install

Next get PIL and untar it

http://effbot.org/downloads/Imaging-1.1.6.tar.gz
tar -zxvf Imaging-1.1.6.tar.gz
cd Imaging-1.1.6

If you already have PIL I would recommend running

sudo rm -Rf build

to clean any existing builds, this has caused me loads of errors and gray hairs!

in your setup.py file run find JPEG_ROOT

amend it so it looks as follows

JPEG_ROOT = libinclude("/usr/local")

Next move onto the build

sudo python setup.py build

if libjpeg is successfully installed you should be able to run python selftest.py without any errors relating to “jpeg”

sudo python setup.py install

if all has worked successfully you should be able to enter your python interpreter by executing python in your command line and also do the following:

import PIL
import Image
import _imaging

without any errors.

Just to triple check I have a simple jpeg on my desktop.

image = Image.open(“/Users/MyName/Desktop/myimage.jpeg”)
image.save(“/Users/MyName/Desktop/test.jpeg”)

should work without errors

ApPeL
Many thanks for your answer. I have read your guide and the only thing from it that I have not done, is the part about using `jpegsrc.v7`I have used libjpeg instead, but maybe I should give this one a try...I can do all the things you suggest at the bottom of your post, without any errors.The problem seems to be that Django tries to import _imaging from the old 2.5 lib instead of the new 2.7 where it is installed.
Mathias Nielsen