我正在尝试将枕头图像转换为 Django ImageField 。
基本上,我:
ImageField
作为输入(来自用户提交的表单)Pillow
这是最后一步,我出现错误'Image' object has no attribute '_committed'
(我相信是Django无法保存Pillow图像,需要转换) >
def upload_media(request):
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
image = form.cleaned_data['image']
pil_image = Image.open(image)
blurred_image = pil_image.filter(ImageFilter.GaussianBlur(100))
post = Post(image=image, blurred_image=blurred_image)
post.save()
return redirect('index')
答案 0 :(得分:0)
请求中的数据已转换为fileupload对象。您需要获取此图像并将其转换为字节,然后使用InMemoryUploadedFile类将其转换为fileupload对象。此类的输出是fileupload对象。
image = Img.open(field)
image = image.convert('RGB')
image = image.resize((800, 800), Img.ANTIALIAS)
output = io.BytesIO()
image.save(output, format='JPEG', quality=85)
output.seek(0)
new_pic= InMemoryUploadedFile(output, 'ImageField',
field.name,
'image/jpeg',
sys.getsizeof(output), None)