Search code examples
pythonflaskpython-imaging-libraryflask-restplus

Flask Restplus returns a corrupted PNG file


Developing a rest API using flask with flask_restplus. It successfully returns an image file, generated by the PIL library, but the file is corrupted and cannot be viewed.

@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')

Solution

  • Please reference to this github issue.

    Swagger basically messes up the encoding. If you make the call though another client it should work properly.