jpg图片无法回复

jpg image cannot put in response

当我在一个Python Flask项目上工作时,我需要将.jpg图像放入响应中,但是,在我使用cv2读取jpg图像后,我将图像信息放入响应中 make_response。报错:"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." 是不是jpg文件的读取方式或编码方式有问题?谢谢

@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

你使用类似的东西

from flask import send_file

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

发回 ok.gif 或 error.gif,具体取决于类型查询参数。有关详细信息,请参阅 send_file 函数和请求对象的文档。