"SimpleServer Python27生成Matplotlib创建的损坏png图像"

2024-04-20 13:32:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python27中创建了SimpleServer,在这里我调用cgi脚本,Matplotlib返回一个.png图像。但是当我尝试用html显示PNG时,它总是坏掉的。你知道吗

我就是这样做的:

imgdata  = StringIO.StringIO()
fig_view.savefig(imgdata, format='png')
imgdata.seek(0)
print "Content-Type: text/html\n"
print """<html><body>
<img src="data:image/png;base64,%s"/>
</body></html>""" % imgdata.getvalue().encode("base64").strip()

但形象总是破碎的 当我试着这样做的时候:

print "Content-Type: image/png\n"            
fig_view.savefig(sys.stdout, format='png')

它显示正常,但我需要在循环中显示一些png文件,这就是为什么我需要使用StringIO(cStringIO),但我不能

请帮帮我!你知道吗


Tags: imageviewformatpnghtmltypefigbody