Dar*_*vor 8 python django matplotlib
我在django应用程序中使用matplotlib,并希望直接返回渲染的图像.到目前为止,我可以去plt.savefig(...),然后返回图像的位置.
我想做的是:
return HttpResponse(plt.renderfig(...), mimetype="image/png")
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
wie*_*rob 17
Django的HttpResponse对象支持类似文件的API,您可以将文件对象传递给savefig.
response = HttpResponse(mimetype="image/png")
# create your image as usual, e.g. pylab.plot(...)
pylab.savefig(response, format="png")
return response
Run Code Online (Sandbox Code Playgroud)
因此,您可以直接在图像中返回图像HttpResponse.
cStringIO怎么样?
import pylab
import cStringIO
pylab.plot([3,7,2,1])
output = cStringIO.StringIO()
pylab.savefig('test.png', dpi=75)
pylab.savefig(output, dpi=75)
print output.getvalue() == open('test.png', 'rb').read() # True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5438 次 |
| 最近记录: |