python—如何使用flask和python3从本地文件读取一些图像,然后显示在网页上?

af7jpaap  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(213)

我想从本地文件路径读取一些图像(.jpg),然后使用flask和python3在网页(127.0.0.1:8000)上显示这些图像。我已经使用以下代码在网页上显示了单个图像:

from flask import Flask,render_template

app = Flask(__name__)

def return_img_stream(img_local_path):
    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 hello_world():
    img_path = 'D:\\1111\\1.jpg'
    img_stream = return_img_stream(img_path)

    return render_template('index.html',img_stream=img_stream)

if __name__ == '__main__':
    app.run(debug=True,host='127.0.0.1',port=8080)

我应该如何修改此代码

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题