I am trying to get a numpy array image from a Matplotlib shape, and currently I am doing this by saving the file and then reading the file again, but I feel there should be a better way. Here is what I am doing now:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.gca()
ax.text(0.0,0.0,"Test", fontsize=45)
ax.axis('off')
canvas.print_figure("output.png")
image = plt.imread("output.png")
I tried this:
image = np.fromstring( canvas.tostring_rgb(), dtype='uint8' )
from the example I found, but it gives me an error saying that the object 'FigureCanvasAgg' does not have the attribute 'renderer'.
source
share