0

When I work on a Python Flask project, I need to put an .jpg image into a response, however, after I use cv2 read the jpg image, I put the image information into response by make_response. I get the error: "TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a ndarray." Is there anything wrong with the read method or encode method of jpg file? Thanks.

@image_proxy_home.route('/images/<int:pid>.jpg')
def get_image(pid):
    img = cv2.imread('/user/'+str(pid)+'.jpg')
    response = make_response(img)
    response.headers.set('Content-Type', 'image/jpeg')
    response.headers.set(
        'Content-Disposition', 'attachment', filename='%s.jpg' % pid)
    return response
QuickLearner
  • 89
  • 6
  • 13
  • You Dont need to read image with cv2; just see this example https://sinafarhadi.ir/How-To-Upload-Files-In-Django/; its for django but the return work for flask tooo – Sina Farhadi Jan 16 '20 at 20:22

1 Answers1

1

You use something like

from flask import send_file

@app.route('/get_image')
def get_image():
    filename = 'myfile.jpg'
    return send_file(filename, mimetype='*/*')

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.

Sina Farhadi
  • 785
  • 4
  • 18