我想保存通过PaletteGenForm上传的图像:
#Form
class PaletteGenForm(forms.Form):
im = forms.ImageField(required=True)
#View
def palette_gen_view(request):
PATH_OF_IMAGE_TO_BE_PALETTED= MEDIA_ROOT+ "/tobesaved.png"
if request.method == 'POST':
form = PaletteGenForm(request.POST, request.FILES)
if form.is_valid():
im = Image.open(StringIO(request.FILES['im']['content']))
im.save(PATH_OF_IMAGE_TO_BE_PALETTED, "PNG")
#call some functions to generate pallete
return #returns the palette of the image.
else:
form = PaletteGenForm()
return render_to_response('palette_generate.html', {'form': form,})
但是,在调用此URL时出现错误:
'InMemoryUploadedFile' object is unsubscriptable
答案 0 :(得分:4)
试试这个:
im = Image.open(StringIO(request.FILES['im'].read()))
答案 1 :(得分:1)
根本不确定是否需要将其包装在StringIO中。尝试
im = Image.open(request.FILES['im']['content'])
答案 2 :(得分:0)
尝试从clean_data表单中读取数据
im = Image.open(StringIO(form.cleaned_data['im'].read()))
对我来说这很有用(没试过请求阅读)