Django和PIL关于雪豹

时间:2021-01-20 00:25:06

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

我正在尝试让PIL与Django 1.2.1和Python 2.7一起使用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.

进口和自我测试。py运行良好,我在交互式python中保存和打开图像,但是Django不能使用它。

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.

为什么PIL似乎在任何地方都可以用,除了Django?我说不通。

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

我甚至在安装libjpeg和PIL之后尝试重新安装Django,但是没有结果,我做错了什么?

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!

编辑:我刚刚发现了一些奇怪的东西。通过在终端中使用交互式python,我可以很好地打开和保存图像。但是由于某种原因,当我保存图像时,颜色是倒过来的!

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.

编辑2:别管最后那部分,看起来我选择的jpg是CMYK颜色的,不能直接保存为RGB或者类似的东西。

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

编辑3:再说一遍,或许Django找错了地方。

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.

这就是Django提出的。我只是看了一下版本,很傻。最上面的一行清楚地表明它正在寻找2.5条路径。奇怪为什么它的2.5,因为SL应该有2.6,哦,好吧,没关系。

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?

有人能告诉我如何指导Django使用更新的吗?改变管理的解决方案。pydid什么都没有。为什么呢,这应该告诉Django不管怎样都要用2.7。对吧?

2 个解决方案

#1


0  

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.

确保您的Django代码引用的是您正在“到处”使用的相同版本的Python。Snow Leopard在/usr/bin/ python.com上默认为Python 2.6.1。

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.

如果您已经通过其他方法安装了Python 2.7,那么它可能会在/usr/bin/python2.7上的另一个路径上找到它的实际位置。如果PIL安装在Python 2.7下,那么您不能在代码中引用/usr/bin/python,因为这会指向错误的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).

最佳实践是显式地指定要在shebang(#!)中使用Python 2.7。为您的Django初始化脚本(例如,manage.py)。

#2


1  

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

我写了一篇关于如何让PIL libjpeg在雪豹上工作的非常广泛的教程。也许这对你有帮助。

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

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

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

go into your shell environment and untar by running the following

进入您的shell环境并运行以下命令来取消tar

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

下一个是PIL并untar

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

如果你已经有了PIL,我建议你跑步。

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

在你的设置。py文件运行找到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”

如果libjpeg成功安装,您应该能够运行python selftest。没有任何与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:

如果一切顺利,您应该能够通过在命令行中执行python来输入您的python解释器,并完成以下工作:

import PIL
import Image
import _imaging

without any errors.

没有任何错误。

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

我在桌面上放了一个简单的jpeg。

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

should work without errors

应该没有错误

#1


0  

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.

确保您的Django代码引用的是您正在“到处”使用的相同版本的Python。Snow Leopard在/usr/bin/ python.com上默认为Python 2.6.1。

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.

如果您已经通过其他方法安装了Python 2.7,那么它可能会在/usr/bin/python2.7上的另一个路径上找到它的实际位置。如果PIL安装在Python 2.7下,那么您不能在代码中引用/usr/bin/python,因为这会指向错误的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).

最佳实践是显式地指定要在shebang(#!)中使用Python 2.7。为您的Django初始化脚本(例如,manage.py)。

#2


1  

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

我写了一篇关于如何让PIL libjpeg在雪豹上工作的非常广泛的教程。也许这对你有帮助。

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

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

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

go into your shell environment and untar by running the following

进入您的shell环境并运行以下命令来取消tar

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

下一个是PIL并untar

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

如果你已经有了PIL,我建议你跑步。

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

在你的设置。py文件运行找到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”

如果libjpeg成功安装,您应该能够运行python selftest。没有任何与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:

如果一切顺利,您应该能够通过在命令行中执行python来输入您的python解释器,并完成以下工作:

import PIL
import Image
import _imaging

without any errors.

没有任何错误。

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

我在桌面上放了一个简单的jpeg。

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

should work without errors

应该没有错误