我的目的是上传图像并进行一些图像处理。现在,我打算渲染上传的图像。
我使用了代码 here为了构建我的前端,我使用 bottle 在 python 中编写了后端,如下所示:
@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)
关于python - 在瓶中渲染来自请求的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580366/