0

我做了什么:

  1. 使用 PyQt5 和 VTK 创建一个应用程序,我在 2 个 QTWidget 中添加了 2 个 VTK 渲染。

  2. 在每个 VTK 渲染中,我读取一个 .stl 文件并显示它。

  3. 我添加了一个函数screen_shot,它可以截屏VTK渲染并保存这些图片。

  4. 我添加了一个菜单栏,我可以单击一个菜单栏和屏幕截图 VTK 渲染。

我想做的事:

我想在加载 VTK 渲染后执行 screen_shot,我把它写在 def __init__ 中。但我以这种方式只能得到 100 * 30 的图片(实际上是 900 * 900)。当我点击菜单时,我会得到一个 900 * 900 的图片。

也许我的描述无法理解,这是我的代码:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)

        self.workspace = ""
        self.data_path = ""
        self.data_index = 0
        self.bkcolor = (1.0, 1.0, 1.0)

        self.frame = QtWidgets.QFrame()
        self.setWindowTitle("PythonVTKViewer")
        # self.frame.setLayout(self.box)

        self.stl_interactor = QtWidgets.QHBoxLayout()
        self.init_menubar()
        self.showMaximized()
        self.load_Interactor()
        self.shot_screen()

    def init_menubar(self):
        # init a menubar
        menubar = self.menuBar()

        fileMenu = menubar.addMenu('&Function')

        self.screen_shot_button = QtWidgets.QAction('ScreenShot', self)
        self.screen_shot_button.setShortcut('Ctrl+S')
        self.screen_shot_button.setStatusTip('ScreenShot')
        self.screen_shot_button.triggered.connect(self.shot_screen)
        fileMenu.addAction(self.screen_shot_button)

    def load_Interactor(self):
        # Create two render windows
        self.vtkWidget_left = QVTKRenderWindowInteractor(self.frame)
        self.vtkWidget_right = QVTKRenderWindowInteractor(self.frame)

        self.stl_interactor.addWidget(self.vtkWidget_left)
        self.stl_interactor.addWidget(self.vtkWidget_right)

        # Left renderer and right renderer
        self.ren_left = vtk.vtkRenderer()
        self.vtkWidget_left.GetRenderWindow().AddRenderer(self.ren_left)
        self.renWinLeft = self.vtkWidget_left.GetRenderWindow()
        self.iren_left = self.renWinLeft.GetInteractor()

        self.ren_right = vtk.vtkRenderer()
        self.vtkWidget_right.GetRenderWindow().AddRenderer(self.ren_right)
        self.iren_right = self.vtkWidget_right.GetRenderWindow().GetInteractor()

        # read stl file
        upper_path = os.path.join(self.workspace, "stl/scanner/start/UpperTeeth.stl")
        lower_path = os.path.join(self.workspace, "stl/scanner/start/LowerTeeth.stl")
        self.init_stl("./temp/upperteeth.stl")
        self.init_stl("./temp/lowerteeth.stl")

        self.frame.setLayout(self.stl_interactor)

        self.setCentralWidget(self.frame)

        self.ren_left.SetBackground(self.bkcolor[0], self.bkcolor[1], self.bkcolor[2])
        self.ren_right.SetBackground(self.bkcolor[0], self.bkcolor[1], self.bkcolor[2])

        # adjust camera
        self.init_camera()

        self.iren_left.Initialize()
        self.iren_right.Initialize()

    def init_stl(self, file):
        # reader = Reader.read_data(file)
        reader = vtk.vtkSTLReader()
        if "upper" in file or "Upper" in file:
            reader.SetFileName(file)
            reader.Update()

            self.mapper_left = vtk.vtkPolyDataMapper()
            self.mapper_left.SetInputConnection(reader.GetOutputPort())
            self.actor_left = vtk.vtkActor()
            self.actor_left.SetMapper(self.mapper_left)
            self.ren_left.AddActor(self.actor_left)
        else:
            reader.SetFileName(file)
            reader.Update()

            self.mapper_right = vtk.vtkPolyDataMapper()
            self.mapper_right.SetInputConnection(reader.GetOutputPort())
            self.actor_right = vtk.vtkActor()
            self.actor_right.SetMapper(self.mapper_right)
            self.ren_right.AddActor(self.actor_right)

    def init_camera(self):
        self.ren_left.GetActiveCamera().SetFocalPoint(0, 0, 0)
        self.ren_left.GetActiveCamera().SetPosition(0, 0, -150)
        self.ren_left.GetActiveCamera().Roll(90)
        self.ren_left.GetActiveCamera().ParallelProjectionOn()
        self.ren_left.GetActiveCamera().SetParallelScale(40)

        self.ren_right.GetActiveCamera().SetFocalPoint(0, 0, 0)
        self.ren_right.GetActiveCamera().SetPosition(0, 0, 150)
        self.ren_right.GetActiveCamera().Roll(270)
        self.ren_right.GetActiveCamera().ParallelProjectionOn()
        self.ren_right.GetActiveCamera().SetParallelScale(40)

    def shot_screen(self):
        if os.path.exists("./temp/input/upper.png"): os.remove("./temp/input/upper.png")
        elif os.path.exists("./temp/input/lower.png"): os.remove("./temp/input/lower.png")

        filter = vtk.vtkRenderLargeImage()
        filter.SetMagnification(1)
        filter.SetInput(self.ren_left)
        writer = vtk.vtkPNGWriter()
        writer.SetFileName("./temp/input/upper.png")
        writer.SetInputConnection(filter.GetOutputPort())
        writer.Write()

        filter = vtk.vtkRenderLargeImage()
        filter.SetMagnification(1)
        filter.SetInput(self.ren_right)
        writer = vtk.vtkPNGWriter()
        writer.SetFileName("./temp/input/lower.png")
        writer.SetInputConnection(filter.GetOutputPort())
        writer.Write()




if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

如何在 def __init__ 中截屏?我需要完成这个功能吗?

4

2 回答 2

1

您需要等到主事件循环开始并且 GUI 已完全呈现,然后再进行屏幕截图。这意味着app.exec_()启动主事件循环的 that 应该在 之前执行MainWindow.shot_screen。从内部实现此目的的一种方法MainWindow.__init__是使用单次触发QTimer延迟执行,MainWindow.shot_screen直到事件循环开始之后,例如

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        ...
        QtCore.QTimer.singleShot(10, self.shot_screen)

    ...
于 2020-01-03T14:21:01.547 回答
0

在你的__init__方法的最后放:

QtWidgets.QApplication.instance().processEvents()
self.shot_screen()

QtWidgets.QApplication.instance()得到你的app. 执行.processEvents()一次事件循环,允许发生@Heike在他的回答中提到的所需初始化。app.exec_()大致相当于:

while windows_are_open:
    app.processEvents()
于 2020-01-09T18:43:12.190 回答