2014-09-22 68 views
0

这是我想要做的。从Matplotlib填充一个瓶子图像

我呼吁模板 “的index.html”,含有某处像这样的图片:

<img class="img-circle" src="/image?imei={{i}}&s={{s}}"> 

在烧瓶,我对/图像URL这样的处理程序:

@app.route('/image', methods=['GET']) 
def image(): 
    imei = request.args.get('imei') 
    dt = request.args.get('s').split("/") 
    return someFunction(imei,dt) <-- SHOULD THIS BE RETURN? 

someFunction生成Matplotlib图像。保存到磁盘时图像看起来是正确的,但我不知道如何“返回”这个图像,以便它在image.html中正确渲染。这里是我目前在做以下(Python Matplotlib to smtplib)什么:

def somefunction(imei,dt) 
    ... 
    #plt is valid and can be saved to disk 
    plt.tight_layout() 
    #plt.show() <-- this looks correct when uncommented 

    buf = io.BytesIO() 
    plt.savefig(buf, format = 'png') 
    buf.seek(0) 
    print(buf.read()) <-- prints a bunch of stuff 
    return buf.read() <-- this does not work 

前述方式“一堆东西”看起来是这样的:

b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x03 \x00\x00\x02X\x08\x06\x00\x00\x00 

我该怎么办呢?目前烧瓶用炸弹:ValueError异常:查看功能并没有返回响应

回答

2

尝试:

@app.route("/image") 
def image(): 
    ret = # create raw image data w/matplotlib 

    resp = Response(response=ret, 
        status=200, 
        mimetype="image/png") 

    return resp 

我没试过,可能无法正常工作。