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

标签 python post python-imaging-library webcam.js

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

我使用了代码 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/

相关文章:

cygwin-1.75 中的 python win32api

python命令将 turtle 变成图像

php - html动态表单,通过post传多个值插入mysql

c# - 尝试创建 HttpContent 时在 Http post 中抛出 InvalidOperationException

python - PIL仅保存第一张图像

python-3.x - 如何遍历所有图像并在python中为所有图像添加边框?

python - 如何从Python线程访问数据

python - 无法使用 python 连接到 *.onion 站点(<urlopen 错误 [Errno 11001] getaddrinfo 失败>)

android - Volley Post JsonObjectRequest 在使用 getHeader 和 getParams 时忽略参数

python - PIL 的舍入误差