2014-09-22 51 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 

我沒試過,可能無法正常工作。