让Django识别PIL JPEG支持

时间:2012-07-27 16:41:48

标签: python django python-imaging-library virtualenv libjpeg

我在使用Django 1.4管理的PIL 1.1.7中运行virtualenvpip。每当我通过管理界面上传JPEG文件时,都会收到以下错误:Upload a valid image. The file you uploaded was either not an image or a corrupted image.

正如许多Ubuntu用户报告的那样,在安装时,PIL错误地在/usr/lib/中查找libjpeg,而其真实位置位于/usr/lib/i386-linux-gnu/。这是照顾;我按照这些帖子中的答案进行了跟踪:

Django ImageField "Upload a valid image. The file you uploaded was either not an image or a corrupted image."

Why can't I upload jpg files to my Django app via admin/?

现在安装的最终输出如下:


PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      linux2 2.7.3 (default, Apr 20 2012, 22:44:07)
              [GCC 4.6.3]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

To check the build, run the selftest.py script.
changing mode of build/scripts-2.7/pilfile.py from 644 to 755
changing mode of build/scripts-2.7/pilfont.py from 644 to 755
changing mode of build/scripts-2.7/pilconvert.py from 644 to 755
changing mode of build/scripts-2.7/pilprint.py from 644 to 755
changing mode of build/scripts-2.7/pildriver.py from 644 to 755

changing mode of /usr/local/bin/pilfile.py to 755
changing mode of /usr/local/bin/pilfont.py to 755
changing mode of /usr/local/bin/pilconvert.py to 755
changing mode of /usr/local/bin/pilprint.py to 755
changing mode of /usr/local/bin/pildriver.py to 755
Successfully installed PIL

但是,Django 仍然不允许我上传除BMP文件以外的任何内容,这是该项目不可接受的。与第二个链接中的用户不同,我没有使用Apache,所以我的问题可能与此无关。我怀疑Django仍在使用旧的PIL安装。问题是我不知道从哪里得到它。 ~/.virtualenvs/project/local/lib/python2.7/site-packages中的文件似乎对我来说是最新的。有什么想法吗?

编辑1:另请注意,我已尝试注销并重新登录管理员,以及安装pillow,如以下帖子所示:Uploading a JPEG image via Django displays error

1 个答案:

答案 0 :(得分:2)

事实证明,由于对pip实际运作方式的误解,这整件事都是我的错。根据习惯,我将任何和所有安装与超级用户权限相关联,这不仅在这种情况下是必需的,而且是混淆的一个秘诀。

我第一次安装PIL时,我没有用sudo作为前缀,但我每次都这样做。因此,根据Meitham的建议,我检查了进口的来源。它来自正确的地方,但它没有我想要的扩展,尽管安装后输出说我做了。长话短说,我从我的网站包中删除了目录,然后pip freeze继续告诉我,我没有PIL,但sudo pip install告诉我,我做了。

获得的经验: virtualenv基于用户的权限,位于用户的目录中。在将sudopip合并到同一命令之前,请三思而后行。