Python Django:上传前调整图像大小
我想在上传之前调整图像的大小(枕头),我在下面写了代码,但不起作用! 并获取错误: 属性错误位于/myapp/list/ _承诺的 申请方式:邮寄 请求URL: Django版本:1.8异常类型:AttributeError异常值: _承诺的 异常位置: /usr/local/lib/python3.4/dist-packages/Pillow-2.8.1-py3.4-linux-x86_64.egg/PIL/Image.py 在getattr中,第622行Python可执行文件:/usr/bin/python3.4 Python 版本:3.4.0 视图.pyPython Django:上传前调整图像大小,python,django,python-imaging-library,Python,Django,Python Imaging Library,我想在上传之前调整图像的大小(枕头),我在下面写了代码,但不起作用! 并获取错误: 属性错误位于/myapp/list/ _承诺的 申请方式:邮寄 请求URL: Django版本:1.8异常类型:AttributeError异常值: _承诺的 异常位置: /usr/local/lib/python3.4/dist-packages/Pillow-2.8.1-py3.4-linux-x86_64.egg/PIL/Image.py 在getattr中,第622行Python可执行文件:/usr/bi
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
imga = request.FILES['docfile']
size = (600, 400)
im = Image.open(imga)
imga = im.resize(size)
request.FILES['docfile'] = imga
newdoc = Document(docfile = request.FILES['docfile'], namefile=request.POST['namefile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('myproject.myapp.views.list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'myapp/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
对于图像大小调整,您可以使用djanof easy缩略图库 下面是我在项目中使用的示例代码
options = {'size': (200, 200), 'crop': True}
thumb_url =get_thumbnailer(image path).get_thumbnail(options).url
供参考
另外,对于调整大小,我使用了“python图像调整大小”有一些有用的答案,但您可能想了解当前代码的情况 您的代码引发该异常,因为这一行:
request.FILES['docfile'] = imga
这有什么不对?您正在将枕头
Image
对象影响到djangoImageField
元素。这是两种不同的类型,当您调用Document
constructor时,它可能会找到一个包含\u committed
属性的文件表单字段。与您的问题无关,而是问题的标题:您知道您正在服务器端调整图像的大小,因此在上传之后,从技术上讲是这样的(而不是之前)。当您只分配self.image=new\u image\u io
request.FILES['docfile'] = imga