在使用python flask 时,网页显示不出图片,只运行html文件可以,但运行py文件就不行,于是上网查资料发现不能直接贴上去
py 文件:
from flask import Flask,render_template
app = Flask(name)
def return_img_stream(image_loca_path): #向前端显示图片时使用,把图片转换成数据流
import base64
image_stream = ''
with open(image_loca_path,"r",encoding="UTF-8") as f: # 图片格式有问题
image_stream = f.read()
image_stream = base64.b64encode(image_stream)
return image_stream
@app.route("/")
def index():
image_path = "templates/images/bc.png"
image_stream = return_img_stream(image_path)
return render_template("index.html",img_stream=image_stream)
if name == "main":
app.run(host="0.0.0.0",debug=True)
html文件:
报错提示:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte