简体   繁体   English

Django和PIL在Snow Leopard上

[英]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 我试图在Snow Leopard上使PIL与Django 1.2.1和Python 2.7一起使用

I have followed instructions I found here on SO and I should be doing it right. 我遵循在SO上找到的说明进行操作,我应该做的正确。

The imports and selftest.py works fine and I both save and open images in the interactive python, but Django cannot use it. imports和selftest.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? 为什么PIL在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? 在安装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. 通过在终端中使用交互式python,我可以很好地打开和保存图像。 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? 那有什么线索可以说明为什么它根本无法在Django中工作吗?

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. 这就是Django推出的。 I just looked at the version, silly me. 我只是看着版本,傻傻的我。 The top line clearly states that it is looking in the 2.5 path. 最上面的行清楚地表明它正在寻找2.5路径。 Wonder why its 2.5 since SL should be born with 2.6, oh well, no matter. 想知道为什么SL的2.5应该与2.6一起诞生,哦,好吧,无论如何。

Can anyone then tell me how to direct Django to use the newer ones? 谁能告诉我如何指导Django使用较新的版本? The solution with changing manage.py did nothing. 更改manage.py的解决方案无济于事。 Why is that, that should have told Django to use 2.7 no matter what.. right? 为什么那样,那应该告诉Django无论如何都使用2.7吧?

I wrote a pretty extensive tutorial on how to get PIL, libjpeg to work on Snow leopard. 我写了一篇相当广泛的教程,介绍如何在雪豹上使用PIL,libjpeg。 Maybe this will help you out. 也许这会帮到你。

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 通过运行以下命令进入外壳环境并解压缩

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并将其解压缩

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 在您的setup.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.py,而不会出现与“ 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 应该可以正常工作

Make sure that your Django code is referencing the same version of Python that you're using "everywhere" else. 确保您的Django代码所引用的Python版本与其他地方使用的Python版本相同。 Snow Leopard comes with Python 2.6.1 by default found at /usr/bin/python . Snow Leopard默认随附于/usr/bin/python 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. 如果您已通过其他方式安装了Python 2.7,则可能在/usr/bin/python2.7处的符号链接的另一路径下找到了它的实际位置。 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下安装了PIL,那么您将无法在代码中引用/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 (eg manage.py ). 最佳实践是在Django初始化脚本(例如manage.py )中明确指定要在shebang中使用Python 2.7( #!/usr/bin/env python2.7 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM