|
1 | 1 | """ |
2 | | -=============================== |
3 | | -A mathtext image as numpy array |
4 | | -=============================== |
5 | | -
|
6 | | -Make images from LaTeX strings. |
| 2 | +======================= |
| 3 | +Convert texts to images |
| 4 | +======================= |
7 | 5 | """ |
8 | 6 |
|
9 | | -import matplotlib.mathtext as mathtext |
10 | | -import matplotlib.pyplot as plt |
| 7 | +from io import BytesIO |
11 | 8 |
|
12 | | -parser = mathtext.MathTextParser("Bitmap") |
13 | | -parser.to_png('test2.png', |
14 | | - r'$\left[\left\lfloor\frac{5}{\frac{\left(3\right)}{4}} ' |
15 | | - r'y\right)\right]$', color='green', fontsize=14, dpi=100) |
| 9 | +from matplotlib.figure import Figure |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +from matplotlib.transforms import IdentityTransform |
| 12 | + |
| 13 | + |
| 14 | +def text_to_rgba(s, *, dpi, **kwargs): |
| 15 | + # To convert a text string to an image, we can: |
| 16 | + # - draw it on an empty and transparent figure; |
| 17 | + # - save the figure to a temporary buffer using ``bbox_inches="tight", |
| 18 | + # pad_inches=0`` which will pick the correct area to save; |
| 19 | + # - load the buffer using ``plt.imread``. |
| 20 | + # |
| 21 | + # (If desired, one can also directly save the image to the filesystem.) |
| 22 | + fig = Figure(facecolor="none") |
| 23 | + fig.text(0, 0, s, **kwargs) |
| 24 | + buf = BytesIO() |
| 25 | + fig.savefig(buf, dpi=dpi, format="png", bbox_inches="tight", pad_inches=0) |
| 26 | + buf.seek(0) |
| 27 | + rgba = plt.imread(buf) |
| 28 | + return rgba |
16 | 29 |
|
17 | | -rgba1, depth1 = parser.to_rgba( |
18 | | - r'IQ: $\sigma_i=15$', color='blue', fontsize=20, dpi=200) |
19 | | -rgba2, depth2 = parser.to_rgba( |
20 | | - r'some other string', color='red', fontsize=20, dpi=200) |
21 | 30 |
|
22 | 31 | fig = plt.figure() |
23 | | -fig.figimage(rgba1, 100, 100) |
24 | | -fig.figimage(rgba2, 100, 300) |
| 32 | +rgba1 = text_to_rgba(r"IQ: $\sigma_i=15$", color="blue", fontsize=20, dpi=200) |
| 33 | +rgba2 = text_to_rgba(r"some other string", color="red", fontsize=20, dpi=200) |
| 34 | +# One can then draw such text images to a Figure using `.Figure.figimage`. |
| 35 | +fig.figimage(rgba1, 100, 50) |
| 36 | +fig.figimage(rgba2, 100, 150) |
| 37 | + |
| 38 | +# One can also directly draw texts to a figure with positioning |
| 39 | +# in pixel coordinates by using `.Figure.text` together with |
| 40 | +# `.transforms.IdentityTransform`. |
| 41 | +fig.text(100, 250, r"IQ: $\sigma_i=15$", color="blue", fontsize=20, |
| 42 | + transform=IdentityTransform()) |
| 43 | +fig.text(100, 350, r"some other string", color="red", fontsize=20, |
| 44 | + transform=IdentityTransform()) |
25 | 45 |
|
26 | 46 | plt.show() |
27 | 47 |
|
|
36 | 56 | # in this example: |
37 | 57 |
|
38 | 58 | import matplotlib |
39 | | -matplotlib.mathtext |
40 | | -matplotlib.mathtext.MathTextParser |
41 | | -matplotlib.mathtext.MathTextParser.to_png |
42 | | -matplotlib.mathtext.MathTextParser.to_rgba |
43 | 59 | matplotlib.figure.Figure.figimage |
| 60 | +matplotlib.figure.Figure.text |
| 61 | +matplotlib.transforms.IdentityTransform |
| 62 | +matplotlib.image.imread |
0 commit comments