温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中flask框架显示图像到web页面的方法

发布时间:2020-07-18 16:21:53 来源:亿速云 阅读:834 作者:小猪 栏目:开发技术

这篇文章主要讲解了Python中flask框架显示图像到web页面的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

代码如下

webfig1.py

from flask import Flask
from flask import render_template
import matplotlib.pyplot as plt
import io
import base64

app = Flask(__name__)

@app.route('/')
def build_plot():
  img = io.BytesIO()
  y = [1,2,3,4,5]
  x = [0,2,1,3,4]
  plt.plot(x,y)
  plt.savefig(img, format='png')
  img.seek(0)

  plot_url = base64.b64encode(img.getvalue()).decode()

  return render_template('plot.html', plot_url=plot_url)

if __name__ == '__main__':
  app.debug = True
  app.run()

plot.html

<!DOCTYPE html>
<html>
<title> Plot</title>
<body>
<img src="data:image/png;base64, {{ plot_url }}">
</body>
</html>

先用py绘制了xy的图像,然后经过几个命令,让其转化为plot_url,在传给plot.html,就可以了

Python中flask框架显示图像到web页面的方法

代码在github:https://github.com/qingnvsue/flask中的webfig文件夹

我自己的程序是在网页输入sin函数的幅度,频率,自变量范围等,然后绘制这个sin函数,让其显示到web页面,如图

Python中flask框架显示图像到web页面的方法

Python中flask框架显示图像到web页面的方法

看完上述内容,是不是对Python中flask框架显示图像到web页面的方法有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI