开发者

Django and PIL on Snow Leopard

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 tha开发者_开发技巧t 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?


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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜