pythonpyqt5vtkqvtkwidget

Using vtkResliceImageViewer or vtkImageViewer2 with Python3+PyQt5


I've written working example for vtkImageViewer:

But any image isn't rendered with vtkImageViewer2 or vtkResliceImageViewer. Have found example based on PyQt4+Python2.7 but wasn't able to get rendered images also (probably because of wrong versions of libraries).

Are there some examples in Python with these classes?

import sys
import vtk
from PyQt5 import QtCore, QtGui
from PyQt5 import QtWidgets

from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor


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

        self.frame = QtWidgets.QFrame()
        self.vl = QtWidgets.QVBoxLayout()
        self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
        self.vl.addWidget(self.vtkWidget)

        pathDicomDir = "/path/to/dicom/data"
        reader = vtk.vtkDICOMImageReader()
        reader.SetDirectoryName(pathDicomDir)
        reader.Update()

        self.viewer = vtk.vtkImageViewer()
        # self.viewer = vtk.vtkImageViewer2()
        # self.viewer = vtk.vtkResliceImageViewer()
        self.viewer.SetInputData(reader.GetOutput())

        self.viewer.SetupInteractor(self.vtkWidget)
        self.viewer.SetRenderWindow(self.vtkWidget.GetRenderWindow())
        self.viewer.Render()
        self.frame.setLayout(self.vl)
        self.setCentralWidget(self.frame)

        self.show()


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

Solution

  • Just use

    self.viewer = vtk.vtkImageViewer2()
    

    and add

    self.vtkWidget.Initialize()
    

    after self.show()