我試圖用 PIL 包顯示我編輯的影像,當我試圖讓它顯示在 html 上<img src=''></img>時它沒有出現任何東西,但我看到檢查元素上的檔案名是<img src="<_io.BytesIO object at 0x000001EDA8F76E00>">. 如何使編輯后的影像正確顯示?
應用程式.py
@app.route("/api/games/shipper/image/", methods=["GET"])
def games_shipper():
... # My stuff up here
image_io = BytesIO()
img.save(image_io, "PNG")
image_io.seek(0)
return render_template('image.html', image_data=image_io)
影像.html
... // My stuff up here
<body>
<center>
<image src="{{ image_data }}"></image>
</center>
</body>
uj5u.com熱心網友回復:
您可以使用該函式從緩沖區中讀取資料getvalue(),然后進行轉換。然后可以將 base64 編碼的資料作為資料 url 傳遞給 src 引數。
from base64 import b64encode
@app.route("/api/games/shipper/image/", methods=["GET"])
def games_shipper():
... # My stuff up here
image_io = BytesIO()
img.save(image_io, 'PNG')
dataurl = 'data:image/png;base64,' b64encode(image_io.getvalue()).decode('ascii')
return render_template('image.html', image_data=dataurl)
uj5u.com熱心網友回復:
您需要在 Base64 中編碼您的影像以img直接在標簽中顯示,請參閱如何在 HTML 中顯示 Base64 影像
在 Flask 模板中顯示影像的傳統方法是將影像保存在 Flask 的static檔案夾中,然后在模板中鏈接到它,如下所示
<body>
<center>
<image src="/static/{{image_name}}.png"></image>
</center>
</body>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/421246.html
標籤:
上一篇:如何使用python和索引號從json檔案中獲取特定資料
下一篇:在熊貓中使用函式時按位置選擇列
