簡體   English   中英

從Matplotlib填充Flask圖像

[英]Populate a Flask image from Matplotlib

這就是我想要做的。

我有一個名為“index.html”的模板,某處包含如下圖像:

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

在燒瓶中,我有一個/ image 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到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

我該怎么做呢? 目前Flask炸彈有:ValueError:View函數沒有返回響應

嘗試:

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

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

    return resp

我沒試過,可能行不通。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM