|
180 | 180 | from warnings import warn |
181 | 181 |
|
182 | 182 | from numpy import inf, isinf |
183 | | - |
| 183 | +import numpy as np |
184 | 184 | from matplotlib.pyparsing import Combine, Group, Optional, Forward, \ |
185 | 185 | Literal, OneOrMore, ZeroOrMore, ParseException, Empty, \ |
186 | 186 | ParseResults, Suppress, oneOf, StringEnd, ParseFatalException, \ |
|
197 | 197 | latex_to_standard, tex2uni, latex_to_cmex, stix_virtual_fonts |
198 | 198 | from matplotlib import get_data_path, rcParams |
199 | 199 |
|
| 200 | + |
| 201 | + |
| 202 | +import matplotlib.colors as mcolors |
| 203 | +import matplotlib._png as _png |
200 | 204 | #################### |
201 | 205 |
|
202 | 206 |
|
@@ -2724,3 +2728,74 @@ def parse(self, s, dpi = 72, prop = None): |
2724 | 2728 | font_output.mathtext_backend = None |
2725 | 2729 |
|
2726 | 2730 | return result |
| 2731 | + |
| 2732 | + def to_mask(self, texstr, dpi=120, fontsize=14): |
| 2733 | + """ |
| 2734 | + return an NxM uint8 alpha ubyte mask array of rasterized tex |
| 2735 | +
|
| 2736 | + ''texstr'' |
| 2737 | + A valid mathtext string, eg r'IQ: $\sigma_i=15$' |
| 2738 | +
|
| 2739 | + ''dpi'' |
| 2740 | + The dots-per-inch to render the text |
| 2741 | +
|
| 2742 | + ''fontsize'' |
| 2743 | + The font size in points |
| 2744 | + """ |
| 2745 | + assert(self._output=="bitmap") |
| 2746 | + prop = FontProperties(size=fontsize) |
| 2747 | + ftimage = self.parse(texstr, dpi=dpi, prop=prop) |
| 2748 | + |
| 2749 | + x = ftimage.as_array() |
| 2750 | + return x |
| 2751 | + |
| 2752 | + def to_rgba(self, texstr, color='black', dpi=120, fontsize=14): |
| 2753 | + """ |
| 2754 | + return an NxMx4 RGBA array of ubyte rasterized tex |
| 2755 | +
|
| 2756 | + ''texstr'' |
| 2757 | + A valid mathtext string, eg r'IQ: $\sigma_i=15$' |
| 2758 | +
|
| 2759 | + ''color'' |
| 2760 | + A valid matplotlib color argument |
| 2761 | +
|
| 2762 | + ''dpi'' |
| 2763 | + The dots-per-inch to render the text |
| 2764 | +
|
| 2765 | + ''fontsize'' |
| 2766 | + The font size in points |
| 2767 | + """ |
| 2768 | + x = self.to_mask(texstr, dpi=dpi, fontsize=fontsize) |
| 2769 | + |
| 2770 | + r, g, b = mcolors.colorConverter.to_rgb(color) |
| 2771 | + RGBA = np.zeros((x.shape[0], x.shape[1], 4), dtype=np.uint8) |
| 2772 | + RGBA[:,:,0] = int(255*r) |
| 2773 | + RGBA[:,:,1] = int(255*g) |
| 2774 | + RGBA[:,:,2] = int(255*b) |
| 2775 | + RGBA[:,:,3] = x |
| 2776 | + return RGBA |
| 2777 | + |
| 2778 | + def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14): |
| 2779 | + """ |
| 2780 | +
|
| 2781 | + ''filename'' |
| 2782 | + A writable filename or fileobject |
| 2783 | +
|
| 2784 | + ''texstr'' |
| 2785 | + A valid mathtext string, eg r'IQ: $\sigma_i=15$' |
| 2786 | +
|
| 2787 | + ''color'' |
| 2788 | + A valid matplotlib color argument |
| 2789 | +
|
| 2790 | + ''dpi'' |
| 2791 | + The dots-per-inch to render the text |
| 2792 | +
|
| 2793 | + ''fontsize'' |
| 2794 | + The font size in points |
| 2795 | +
|
| 2796 | + """ |
| 2797 | + |
| 2798 | + rgba = self.to_rgba(texstr, color=color, dpi=dpi, fontsize=fontsize) |
| 2799 | + numrows, numcols, tmp = rgba.shape |
| 2800 | + return _png.write_png(rgba.tostring(), numcols, numrows, filename) |
| 2801 | + |
0 commit comments