【发布时间】:2011-12-12 18:53:58
【问题描述】:
我正在使用图片裁剪器,但我的表单验证遇到了问题。上传 GIF 图片时会验证表单,但我尝试的任何其他格式都会引发以下异常:
Upload a valid image. The file you uploaded was either not an image or a corrupted image.
表格
<form enctype="multipart/form-data" method="post" action="/pic/">{% csrf_token %}
{{ form.as_p }}
<p><input type="submit" value="View uploaded image"></p>
<input type="hidden" name="stage" value="crop">
</form>
查看
if request.method == 'POST':
form = ProfilePicForm(request.POST, request.FILES)
if form.is_valid():
*do stuff*
else:
logger.debug('Form errors == [%s]' % form.errors)
在安装 PIL(使用 easy_install)之前,我已经安装了 libjpeg-dev(使用 apt-get)。起初,我认为这可能是由于 libjpeg-dev 或 PIL 安装不正确,但对于其他格式(如 png),问题是否仍然存在?这个问题实际上也发生在 png 图像上。我没有遇到 jpeg 解码器问题,所以我认为不是这样,但我不确定。另外,我正在使用 django 开发服务器。
更新
我决定尝试重新安装 PIL。我删除了我的 PIL 安装文件夹
/usr/local/lib/python2.7/dist-packages/PIL
和我的 PIL.pth 文件(在同一个 dist-packages 文件夹中)。我使用sudo python setup.py install 运行了 setup.py 脚本。然后我运行了 selftest.py 脚本,但它在第一次测试中失败了:
--------------------------------------------------------------------
PIL 1.1.7 TEST SUMMARY
--------------------------------------------------------------------
Python modules loaded from ./PIL
Binary modules loaded from ./PIL
--------------------------------------------------------------------
--- PIL CORE support ok
*** TKINTER support not installed
*** JPEG support not installed
*** ZLIB (PNG/ZIP) support not installed
*** FREETYPE2 support not installed
--- LITTLECMS support ok
--------------------------------------------------------------------
Running selftest:
*****************************************************************
Failure in example:
try:
_info(Image.open(os.path.join(ROOT, "Images/lena.jpg")))
except IOError, v:
print v
from line #24 of selftest.testimage
Expected: ('JPEG', 'RGB', (128, 128))
Got: decoder jpeg not available
1 items had failures:
1 of 57 in selftest.testimage
***Test Failed*** 1 failures.
*** 1 tests of 57 failed.
我不确定如何让 PIL 使用上面列出的不受支持的模块。我可以验证我有 zlib1g-dev、libfreetype6-dev、liblcms1-dev 和 libjpeg62-dev,以及库本身安装在我的系统上,因为当我尝试使用 apt-get 安装它们时,它说我已经有了最新版本.
【问题讨论】:
-
会不会是你没有安装libjpeg?通常,如果出现问题,PNG 将也能正常工作。
-
当我运行 sudo apt-get install libjpeg-dev 时,输出显示 libjpeg62-dev 已经是最新版本。是否有其他方法可以验证 PIL 和 libjpeg-dev 是否可以正常协同工作?
-
检查是否有:zlib1g-dev、libfreetype6-dev、liblcms1-dev 和 libjpeg62-dev,+ 库本身。
-
强制安装 PIL 后,尝试从 python 解释器中使用 PIL。 (或尝试使用它之前):)
-
我尝试在 Python 解释器中打开并验证文件本身。当我对这些文件(我正在测试的 .png 和 .jpg 图像)运行 verify() 时,我没有收到任何错误。不过,我会检查我是否有这些库。谢谢你们的帮助,伙计们!
标签: python django python-imaging-library libjpeg