MathText in Python vtk 8.1.2
It looks like as of 8.1.2, equations or symbols inclosed in $
in text aren't being rendered as math symbols or equations:
>>> import vtk
>>> vtk_mathtext = vtk.vtkMathTextFreeTypeTextRenderer()
>>> print(vtk_mathtext.MathTextIsSupported())
False
This might have to do with this relying on matplotlib
and vtk probably doesn't want to include that in the wheel.
Regardless, it's still at least possible to get a rendered equation using matplotlib
within python into vtkImageData, but I'm not sure where to go from here. In the source this was a necessary step, but it's unclear how this relates to a vtkTextActor
.
from io import BytesIO
from PIL import ImageTk, Image
import numpy as np
from matplotlib import mathtext
import vtk
import vtki
text = '$\\theta$'
# render math text as an image
buffer = BytesIO()
mathtext.math_to_image(text, buffer, dpi=1000, format='png')
pimage= Image.open(buffer)
raw = buffer.read()
data = np.frombuffer(raw, np.uint8)
image = vtk.vtkImageData()
image.SetDimensions(pimage.size[0], pimage.size[1], 1)
from vtk.util.numpy_support import numpy_to_vtk
vtkarr = numpy_to_vtk(data, deep=True)
image.GetPointData().AddArray(vtkarr)