Extract equation to .png file using Python

I want to display equations in PNG files and embed them in the HTML documentation of my library. I already use pylab (matplotlib) in other projects.

I did not find any clues in http://matplotlib.sourceforge.net/users/usetex.html and http://matplotlib.sourceforge.net/users/mathtext.html

When i do

plt.title(r'$\alpha > \beta$')
plt.show()

I get a headed empty figure with axes.

Update:

After a series of studies, I found that the easiest way to render LaTeX for png is to use mathext ( http://code.google.com/p/mathtex/ ).

Surprisingly, I had all the necessary libraries for creating it from the source.

Anyway, thanks to everyone for the answers.

Update 2:

mathtex , (\ begin {pmatrix}) , . , LaTex (MikTeX).

3:

proTeXt. , . , .

+5
4
  • (source). IPython, matplotlib .

    , plt.title(r'$\alpha > \beta$') IPython .show(). , /cmd/IDLE .

  • plt.show() , IPython , :

    plt.savefig('filename.png')
    

: , . @Li-aung Yip, Sympy . matplotlib , (, ):

import matplotlib.pyplot as plt

#add text
plt.text(0.01, 0.8, r'$\alpha > \beta$',fontsize=50)

#hide axes
fig = plt.gca()
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.draw() #or savefig

.

... "" :\ -, , PIL.

+2

:

# https://gist.github.com/tonyseek/95c90638cf43a87e723b

from cStringIO import StringIO

import matplotlib.pyplot as plt

def render_latex(formula, fontsize=12, dpi=300, format_='svg'):
    """Renders LaTeX formula into image.
    """
    fig = plt.figure(figsize=(0.01, 0.01))
    fig.text(0, 0, u'${}$'.format(formula), fontsize=fontsize)
    buffer_ = StringIO()
    fig.savefig(buffer_, dpi=dpi, transparent=True, format=format_, bbox_inches='tight', pad_inches=0.0)
    plt.close(fig)
    return buffer_.getvalue()

if __name__ == '__main__':
    image_bytes = render_latex(
        r'\theta=\theta+C(1+\theta-\beta)\sqrt{1-\theta}succ_mul',
        fontsize=10, dpi=200, format_='png')
    with open('formula.png', 'wb') as image_file:
        image_file.write(image_bytes)
+6

, LaTeX . . . ( matplotlib, .)

, LaTeX LaTeX, LaTeX postscript .

+2

@warvariuc Python 3 . , . -, StringIO cStringIO Py3. -, io.StringIO , , MatPlotLib. : http://matplotlib.1069221.n5.nabble.com/savefig-and-StringIO-error-on-Python3-td44241.html. , , io.BytesIO. getvalue() , StringIO. savefig , , :

from io import BytesIO
import matplotlib.pyplot as plt

def renderLatex(formula, fontsize=12, dpi=300, format='svg', file=None):
    """Renders LaTeX formula into image or prints to file.
    """
    fig = plt.figure(figsize=(0.01, 0.01))
    fig.text(0, 0, u'${}$'.format(formula), fontsize=fontsize)

    output = BytesIO() if file is None else file
    with warnings.catch_warnings():
        warnings.filterwarnings('ignore', category=MathTextWarning)
        fig.savefig(output, dpi=dpi, transparent=True, format=format,
                    bbox_inches='tight', pad_inches=0.0, frameon=False)

    plt.close(fig)

    if file is None:
        output.seek(0)
        return output

The warning was something that I'm sure is related to the size of the figure. You can remove the attached withif you want. The reason for the search is to make the "file" readable (best explained here: fooobar.com/questions/207621 / ... ).

0
source

All Articles