【发布时间】:2016-11-06 02:33:35
【问题描述】:
您好,我是 Django 的新手(而且我的母语不是英语)。我正在尝试从数据库(ImageField)中获取图像(我使用表单通过 POST 获取图像的 ID),使用 PIL Image 对其进行裁剪并通过 HttpResponse 发送。它没有给出错误,但是当我收到图像“piece.jpg”时,它是一个损坏的文件。如果我使用记事本打开它,我只能看到:
<PIL.Image.Image image mode=RGB size=46x75 at 0x3DEA790>
这是我在views.py中的代码:
from PIL import Image
def function(request):
if request.method == 'POST':
id = request.POST.get('id')
object = Document.objects.get(pk=id)
im = Image.open(object.docfile)
left = 10
top = 10
right = 100
bottom = 100
cropped_image = im.crop( (left, top, right, bottom) )
response = HttpResponse(cropped_image, content_type='image/jpg')
response['Content-Disposition'] = 'attachment; filename="piece.jpg"'
return response
我尝试只发送“object.docfile”并且它有效。但是如果我尝试发送“im”(这是一个 PIL 图像),它就不起作用了。
我需要通过HttpResponse 发送它,因为我希望它可以在该页面上下载。
【问题讨论】:
标签: python django image python-imaging-library crop