您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用python动态将matplotlib图像提供给网络

使用python动态将matplotlib图像提供给网络

你应该

因此,如果发生错误savefig,您仍然可以返回其他内容,甚至另一个标头。有些错误不会更早地识别出来,例如文本问题,图像尺寸太大等。

您需要告诉savefig输出写入何处。你可以做:

format = "png"
sio = cStringIO.StringIO()
pyplot.savefig(sio, format=format)
print "Content-Type: image/%s\n" % format
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) # Needed this on windows, IIS
sys.stdout.write(sio.getvalue())

如果要将图像嵌入HTML:

print "Content-Type: text/html\n"
print """<html><body>
...a bunch of text and html here...
<img src="data:image/png;base64,%s"/>
...more text and html...
</body></html>""" % sio.getvalue().encode("base64").strip()
python 2022/1/1 18:46:05 有316人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶