[英]Flask Restplus returns a corrupted PNG file
Developing a rest API using flask with flask_restplus. 使用flask带flask_restplus开发rest API。 It successfully returns an image file, generated by the PIL library, but the file is corrupted and cannot be viewed. 它成功返回了由PIL库生成的图像文件,但是该文件已损坏并且无法查看。
@api.route('/annotate')
class Annotate(Resource):
@api.representation('image/png')
def post(self):
file = io.BytesIO()
img = Image.new('RGBA', (50, 50), (70, 0, 0, 255))
img.save(file, 'png')
file.seek(0)
return send_file(file,
as_attachment=True,
attachment_filename='annotated.png',
mimetype='image/png')
It seems to be working just fine. 它似乎工作正常。 I jus tried the following and it works as expected. 我可以尝试以下方法,它可以按预期工作。 Here is the resulting image as well. 这也是生成的图像。
from flask import Flask, render_template, jsonify, send_file
from PIL import Image
import io
app = Flask(__name__)
@app.route('/image')
def image():
file = io.BytesIO()
img = Image.new('RGBA', (50, 50), (70, 0, 0, 255))
img.save(file, 'png')
file.seek(0)
return send_file(
file,
as_attachment=True,
attachment_filename='annotated.png',
mimetype='image/png')
Start it like so FLASK_APP=f1.py flask run
. 像启动FLASK_APP=f1.py flask run
一样启动它。
Please reference to this github issue . 请参考这个github问题 。
Swagger basically messes up the encoding. Swagger基本上弄乱了编码。 If you make the call though another client it should work properly. 如果您通过另一个客户拨打电话,它应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.