views:

39

answers:

3

I want to save the image which as been uploaded via the PaletteGenForm as such:

#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,})

However here is my error when calling this URL:

'InMemoryUploadedFile' object is unsubscriptable
+2  A: 

Try this:

im = Image.open(StringIO(request.FILES['im'].read()))
Alexander Artemenko
Thanks for the suggestion, here is what I get after changing to yours:TypeError at /palet/'module' object is not callable
Hellnar
looks like you are having 'import StringIO' in your code instead of 'from StringIO import StringIO'.
Ashok
A: 

Not sure you need to wrap it in a StringIO at all. Try

im = Image.open(request.FILES['im']['content'])
Daniel Roseman
without wrapping I still get the same error:TypeError at /palet/'InMemoryUploadedFile' object is unsubscriptable
Hellnar
A: 

try reading the data from form cleaned_data

im = Image.open(StringIO(form.cleaned_data['im'].read()))

for me this worked (didn't tried reading from the request)

Ashok
this is same as Alexander Artemenko answer,can you provide the complete traceback to see which line is giving the error: TypeError at /palet/ 'module' object is not callable
Ashok