Show More
@@ -111,7 +111,8 b" def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black'," | |||
|
111 | 111 | |
|
112 | 112 | def latex_to_png_mpl(s, wrap, color='Black', scale=1.0): |
|
113 | 113 | try: |
|
114 | from matplotlib import mathtext | |
|
114 | from matplotlib import figure, font_manager, mathtext | |
|
115 | from matplotlib.backends import backend_agg | |
|
115 | 116 | from pyparsing import ParseFatalException |
|
116 | 117 | except ImportError: |
|
117 | 118 | return None |
@@ -122,11 +123,18 b" def latex_to_png_mpl(s, wrap, color='Black', scale=1.0):" | |||
|
122 | 123 | s = u'${0}$'.format(s) |
|
123 | 124 | |
|
124 | 125 | try: |
|
125 | mt = mathtext.MathTextParser('bitmap') | |
|
126 | f = BytesIO() | |
|
126 | prop = font_manager.FontProperties(size=12) | |
|
127 | 127 | dpi = 120*scale |
|
128 | mt.to_png(f, s, fontsize=12, dpi=dpi, color=color) | |
|
129 | return f.getvalue() | |
|
128 | buffer = BytesIO() | |
|
129 | ||
|
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 | 138 | except (ValueError, RuntimeError, ParseFatalException): |
|
131 | 139 | return None |
|
132 | 140 |
General Comments 0
You need to be logged in to leave comments.
Login now