使用 PIL 图像打开 POSTed 文件
使用 WSGI、webob 和 PIL,我尝试直接从请求对文件使用 Image.open()
。然而,Image.open()
总是抛出异常“无法识别图像文件”。图像是唯一的字段,不使用其他 POST 或 GET 变量。该文件来自标准 HTML 上传表单,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')
我的猜测是我没有正确加载上传的图像数据,但不确定正确的方法是什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 webob,但我的猜测是 body_file 包含整个帖子的内容,而不仅仅是您的图像。 文档似乎证实了这一点。
req.POST['nameOfFileControl'] 中有什么?那有文件句柄吗?这将是 Image.open 需要的文件句柄。
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.