我正在开发一个图片裁剪工具,但在表单验证方面遇到了问题。当上传GIF图片时,表单可以通过验证,但是尝试上传其他格式的图片时会引发以下异常:
我在安装PIL之前使用apt-get安装了libjpeg-dev。一开始,我认为这可能是由于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脚本,但它在第一个测试上失败了。
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之前使用apt-get安装了libjpeg-dev。一开始,我认为这可能是由于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安装时,它会提示我已经安装了最新版本。