从 Flask 中的 HTTP POST 请求读取图像文件并将其转换为 PIL 图像

Nuw*_*wan 2 python python-imaging-library flask python-3.x

我有一个带有 POST 方法的简单烧瓶服务器,可以从用户那里获取图像。获取图像后,我需要将该图像转换为 PIL 图像文件,其余过程需要该 PIL 图像文件。

@app.route("/predict",methods=["POST"])
def predict():
    image=request.files['file']
    # convert this image to pil image
    .....
Run Code Online (Sandbox Code Playgroud)

有什么简短的方法吗?

Nuw*_*wan 5

图像可以传递给 Image.open(your_image) 函数。

pil_image = Image.open(image)
Run Code Online (Sandbox Code Playgroud)