Fix Python – How to return images in flask response?


Asked By – wong2

As an example, this URL:

should return a response with a image/gif MIME type. I have two static .gif images,
and if type is 1, it should return ok.gif, else return error.gif. How to do that in flask?

Now we will see solution for issue: How to return images in flask response?


You use something like

from flask import send_file

def get_image():
    if request.args.get('type') == '1':
       filename = 'ok.gif'
       filename = 'error.gif'
    return send_file(filename, mimetype='image/gif')

to send back ok.gif or error.gif, depending on the type query parameter. See the documentation for the send_file function and the request object for more information.

This question is answered By – Martin Geisler

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0