3

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.

Derek Dahmer
  • 14,865
  • 6
  • 36
  • 33
  • When you say it's “POSTed”, do you mean you're using a special tool to POST a request with body `Content-Type: image/png`? Or do you just mean you've got a browser and a `multipart/form-data` form with only one field, an ``? If the latter then you still need to parse the form in the usual way to get the file upload field out of it. – bobince Feb 01 '10 at 14:39
  • It's the latter, this is a standard file upload form in HTML. – Derek Dahmer Feb 01 '10 at 17:30

1 Answers1

4

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
  • 106,305
  • 20
  • 172
  • 230