new to PIL, but want to get a quick solution out of it. The following is my first shot which never works:
import cStringIO
import pylab
from PIL import Image
I like having it encapsulated in a function:
def fig2img(fig):
"""Convert a Matplotlib figure to a PIL Image and return it"""
import io
buf = io.BytesIO()
fig.savefig(buf)
buf.seek(0)
img = Image.open(buf)
return img
Then I can call it easily this way:
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
x = np.arange(-3,3)
plt.plot(x)
fig = plt.gcf()
img = fig2img(fig)
img.show()