1.通过静态文件加载
<img src="./test.png">
2.通过字节传递二进制图片文件
from jinja2 import Template,FileSystemLoader,Environment
env = Environment(loader=FileSystemLoader("./"))
template = env.get_template("./test.html")
import matplotlib.pyplot as plt
x = [1,2,3]
y = [1,2,3]
plt.plot(x,y)
from io import BytesIO
buf = BytesIO() #另一种写入BytesIO的方法
plt.savefig(buf,format='png') #另一种写入BytesIO的方法
import base64
data = base64.b64encode(buf.getvalue()).decode()
data = "data:image/png;base64," + data
temp_render = template.render(name=data)
print(temp_render)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img src="{{ name }}">
</body>
</html>
3.通过get请求
img
标签中的src
属性本质是向服务器发送一个get请求
@app.route('/<img_id>')
def capt(img_id):
# 生成图片验证码
text, image = captcha.generate_captcha()
rsp = make_response(image)
rsp.mimetype = 'image/jpg'
return rsp
<img src="{{ img_id }}" alt="图形验证码" class="pic_code">