s, (width, height) = agg.print_to_buffer()
// Convert to a NumPy array.
X = np.frombuffer(s, np.uint8).reshape((height, width, 4))
// Pass off to PIL.
from PIL import Image
After Change
agg = canvas.switch_backends(FigureCanvasAgg)
agg.draw()
X = np.asarray(agg.buffer_rgba())
// Pass off to PIL.
from PIL import Image
im = Image.fromarray(X)