Show More
@@ -111,7 +111,8 b" def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black'," | |||||
111 |
|
111 | |||
112 | def latex_to_png_mpl(s, wrap, color='Black', scale=1.0): |
|
112 | def latex_to_png_mpl(s, wrap, color='Black', scale=1.0): | |
113 | try: |
|
113 | try: | |
114 | from matplotlib import mathtext |
|
114 | from matplotlib import figure, font_manager, mathtext | |
|
115 | from matplotlib.backends import backend_agg | |||
115 | from pyparsing import ParseFatalException |
|
116 | from pyparsing import ParseFatalException | |
116 | except ImportError: |
|
117 | except ImportError: | |
117 | return None |
|
118 | return None | |
@@ -122,11 +123,18 b" def latex_to_png_mpl(s, wrap, color='Black', scale=1.0):" | |||||
122 | s = u'${0}$'.format(s) |
|
123 | s = u'${0}$'.format(s) | |
123 |
|
124 | |||
124 | try: |
|
125 | try: | |
125 | mt = mathtext.MathTextParser('bitmap') |
|
126 | prop = font_manager.FontProperties(size=12) | |
126 | f = BytesIO() |
|
127 | dpi = 120 * scale | |
127 | dpi = 120*scale |
|
128 | buffer = BytesIO() | |
128 | mt.to_png(f, s, fontsize=12, dpi=dpi, color=color) |
|
129 | ||
129 | return f.getvalue() |
|
130 | # Adapted from mathtext.math_to_image | |
|
131 | parser = mathtext.MathTextParser("path") | |||
|
132 | width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop) | |||
|
133 | fig = figure.Figure(figsize=(width / 72, height / 72)) | |||
|
134 | fig.text(0, depth / height, s, fontproperties=prop, color=color) | |||
|
135 | backend_agg.FigureCanvasAgg(fig) | |||
|
136 | fig.savefig(buffer, dpi=dpi, format="png", transparent=True) | |||
|
137 | return buffer.getvalue() | |||
130 | except (ValueError, RuntimeError, ParseFatalException): |
|
138 | except (ValueError, RuntimeError, ParseFatalException): | |
131 | return None |
|
139 | return None | |
132 |
|
140 |
General Comments 0
You need to be logged in to leave comments.
Login now