在瓶中渲染来自请求的图像

Render image from request in bottle

我的目的是上传一张图片并做一些图片处理。现在,我打算渲染上传的图像。

我使用代码here构建了我的前端,我在python中使用bottle编写了后端,如下所示:

@route('/test', method='POST')
def serve_image():
    # import pdb; pdb.set_trace()
    image = Image.open(request.body)
    image.show()

我得到如下错误

OSError: cannot identify image file <_io.BytesIO object at 0x0000017386B53A40>

我错过了什么?

编辑: 当我打印整个请求时,这就是我得到的

< http://localhost:8080/test>

那个教程不是很全面,但是full documentation更有用:

The image data is uploaded as part of a standard multipart form post, and included as a form element named webcam.

因此,与其尝试将整个请求正文传递给 Pillow,不如只传递该元素,使用 request.files multidict,并访问其 file 属性以获取缓冲区:

image = Image.open(request.files['webcam'].file)