import matplotlib.mathtext as mt s=r'$\frac{A}{B} = C$' mt.math_to_image(s, 'test.png')

the second argument to math_to_image could also be a BytesIO, etc.