python - 如何将背景颜色放在 vtktextactor 后面?

标签 python background vtk

我正在尝试在用户点击的图像上显示一些上下文信息。

我正在为此使用 vtkTextActor。我正在使用 python 绑定(bind)。

问题是因为它在图像上很难阅读,所以我想在它后面画一些背景。 (也许是一个小方形多数据,一些简单的东西)。但我需要知道文本范围是什么,我不知道如何使用 text.GetSize()

最佳答案

最简单的解决方案可能是使用成员函数 SetBackgroundColorSetBackgroundOpacityvtkTextPropertyvtkTextActor .

使用text.GetSize()对后台来说不是必须的,但是如果有用的话,可以调用GetBoundingBox(renderer, vector)vtkTextActor .

参见 vtkTextProperty Class Reference

演示:

Demo of vtkTextActor with background and border

# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.vtkRenderingFreeType    # necessary for vtkTextActor

from vtkmodules.vtkRenderingCore import (
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer,
)
from vtkmodules.vtkRenderingCore import vtkTextActor

# window initialization
renderer = vtkRenderer()
render_window = vtkRenderWindow()
render_window.SetSize([800, 150])
render_window.AddRenderer(renderer)
interactor = vtkRenderWindowInteractor()
interactor.SetRenderWindow(render_window)

# construct the text object
text_actor = vtkTextActor()
text_actor.SetInput("Hello vtkTextActor()\n     also you might set SetFrameColor.")                        # you can have multi-line text
prop = text_actor.GetTextProperty()
prop.SetFontSize(28)
prop.SetFontFamilyToCourier()
prop.SetColor(1.0, 1.0, 0.0)
prop.SetBackgroundColor(0.0, 0.2, 0.5)  # set the background color
prop.SetBackgroundOpacity(1.0)          # 1.0 = opaque, 0.0 = transparent
#prop.SetBackgroundRGBA(0.0, 0.2, 0.5, 0.5)  # alternative
prop.FrameOn()                          # bounding box
text_actor.SetPosition(50, 50)
renderer.AddActor2D(text_actor)

# present the window
interactor.Initialize()
renderer.ResetCamera()
render_window.Render()
interactor.Start()

关于python - 如何将背景颜色放在 vtktextactor 后面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24049802/

相关文章:

c++ - ItkVtkGlue 的问题

python - 在 Python 中使用自定义排序参数对列表进行排序的最佳方法是什么?

python - 如何在pygame中添加背景图像?

python - Python pickle 的首选(或最常见)文件扩展名

java - 无法设置 JApplet 背景颜色

javascript - 带填充的 DIV 背景定位

c# - VTK ActiViz 将对象重置为原始 View

c++ - 如何自动关闭打开的 vtk 窗口?

python - 类型错误 : wrapper() takes 1 positional argument but 2 were given

python - xlsxwriter:如何在单元格中居中图像