【发布时间】:2017-01-06 07:23:46
【问题描述】:
试图将一个简单的图(保存在 StringIO 中)返回到 Web 浏览器。经过几个小时的阅读,也许终于接近了。
import cgi
import cStringIO
import matplotlib.pyplot as plt
import numpy as np
def doit():
x = np.linspace(-2,2,100)
y = np.sin(x)
format = "png"
ggg = cStringIO.StringIO()
plt.plot(x, y)
plt.savefig(ggg, format=format)
data_uri = ggg.read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}" alt="thisistheplot"/>'.format(data_uri)
print("Content-type: text/html\n")
print("<title>Try Ageen</title>")
print("<h1>Hi</h1>")
print(img_tag)
doit()
它返回一个损坏的图像图标。我已经看过了:Dynamically serving a matplotlib image to the web using python,等等……
【问题讨论】:
标签: python string image matplotlib cgi