【发布时间】:2020-02-12 00:36:52
【问题描述】:
我想将多张图片转换成base64并以json格式返回。同时,我想在我的 html 模板中显示。当我运行我的代码时,它运行良好,但结果只显示某些结果。下面附上我的结果。我也无法在 html 模板中显示我的 json 数据。这里附上我的代码。
app.py
@app.route("/api/images")
def get_images():
directory = os.listdir('C:/Users/HP/Miniconda3/envs/count_vechicle/coding/images')
os.chdir('C:/Users/HP/Miniconda3/envs/count_vechicle/coding/images')
data={}
for file in directory:
base = os.path.basename(file)
data["label"] = base
open_file = open(file,'rb')
image_read = open_file.read()
image_64_encode = base64.encodebytes(image_read)
data["data"] = image_64_encode.decode('ascii')
final_data = json.dumps(data,sort_keys = True, indent = 4, separators = (',', ': '))
print(final_data)
#return final_data
return render_template("images.html", final_data=final_data)
Images.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1 class="logo">Results</h1>
<ul>
{% for data in final_data %}
<li>{{final_data}}</li>
<img src={{data.da}}>
{% endfor %}
</ul>
</body>
</html>
【问题讨论】:
标签: python json python-3.x flask base64