python matplotlib 保存图片_Python将matplotlib图保存在PIL图像对象上

HI, is it possible that I created a image from matplotlib and I save it on an image object I created from PIL? Sounds very hard? Who can help me?

解决方案

To render Matplotlib images in a webpage in the Django Framework:

create the matplotlib plot

save it as a png file

store this image in a string buffer (using PIL)

pass this buffer to Django's HttpResponse (set mime type image/png)

which returns a response object (the rendered plot in this case).

In other words, all of these steps should be placed in a Django view function, in views.py:

from matplotlib import pyplot as PLT

import numpy as NP

import StringIO

import PIL

from django.http import HttpResponse

def display_image(request) :

# next 5 lines just create a matplotlib plot

t = NP.arange(-1., 1., 100)

s = NP.sin(NP.pi*x)

fig = PLT.figure()

ax1 = fig.add_subplot(111)

ax1.plot(t, s, 'b.')

buffer = StringIO.StringIO()

canvas = PLT.get_current_fig_manager().canvas

canvas.draw()

pil_image = PIL.Image.fromstring('RGB', canvas.get_width_height(),

canvas.tostring_rgb())

pil_image.save(buffer, 'PNG')

PLT.close()

# Django's HttpResponse reads the buffer and extracts the image

return HttpResponse(buffer.getvalue(), mimetype='image/png')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值