views:

130

answers:

1

Using WSGI, webob and PIL, I'm trying to use Image.open() on a file directly from the request. However, Image.open() always throws the exception "cannot identify image file". The image is the only field, no other POST or GET variables are used. The file is coming from a standard HTML upload form with enctype="multipart/form-data".

import Image, ImageFile
from webob import Request

def application(environ, start_response):
    req = Request(environ)
    req.make_body_seekable() 
    im = Image.open(req.body_file) # "Cannot identify image file"
    im.save('testfileio.png','PNG')

My guess is I'm not loading in the uploaded image data correctly, but am not sure what the right way to do it would be.

+1  A: 

I'm not famaliar with webob, but my guess is that body_file contains the contents of the entire post and not just your image. The docs seem to confirm this.

What's in req.POST['nameOfFileControl']? Does that have a file handle? That is going to be the file handle that Image.open needs.

Mark