#encoding:utf-8
from flask import Flask,render_template
app = Flask(__name__)
def return_img_stream(img_local_path):
"""
工具函数:
获取本地图片流
:param img_local_path:文件单张图片的本地绝对路径
:return: 图片流
"""
import base64
img_stream = ''
with open(img_local_path, 'rb') as img_f:
img_stream = img_f.read()
img_stream = base64.b64encode(img_stream).decode()
return img_stream
@app.route('/')
def hellod():
img_path = './templates/1.jpg'
img_stream = return_img_stream(img_path)
return render_template('index1.html',
img_stream=img_stream)
if __name__ == '__main__':
app.run(host='localhost', port=8080,debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flask Show Image</title>
</head>
<body>
<img style="width:180px" src="data:;base64,{{ img_stream }}">
</body>
</html>
index1.html要放入templates文件夹下