from io import BytesIO import matplotlib.pyplot as plt def get_png(tex): buf = BytesIO() plt.text(0, 0.6, r"$%s$" % tex, fontsize = 50) fig = plt.gca() fig.axes.get_xaxis().set_visible(False) fig.axes.get_yaxis().set_visible(False) plt.savefig(buf) buf.seek(0) return buf