I’m trying to use vtkWindowToImageFilter to paint a single view of an object into a widget in my Qt application. The closest I can get is this (still wrong):
# generate the view
renderWin = vtk.vtkRenderWindow()
renderWin.OffScreenRenderingOn()
renderWin.AddRenderer(self.renderer)
renderWin.Render()
# copy the view to an image
windowToImageFilter = vtk.vtkWindowToImageFilter()
windowToImageFilter.SetInput(renderWin)
windowToImageFilter.ReadFrontBufferOff(); # read from the back buffer
windowToImageFilter.Update()
img = windowToImageFilter.GetOutput()
w, h, _ = img.GetDimensions()
vtk_array = img.GetPointData().GetScalars()
components = vtk_array.GetNumberOfComponents()
# DEBUG: save image
writer = vtk.vtkPNGWriter()
writer.SetFileName("window_{}.png".format(view.name))
writer.SetInputConnection(windowToImageFilter.GetOutputPort())
writer.Write()
# put the image on the window
qim = QImage(vtk_array, w, h, QImage.Format_RGB32)
qPixmap = QPixmap.fromImage(qim)
window.setPixmap(qPixmap)
The images are ok when saved, but what I get in the window is a garbled image, because there is clearly a format mismatch between vtk (24-bit image) and Qt (32-bit image), and I can’t find a way to pass the information about the different strides.
Is there a “proper” way to do the conversion, which I haven’t found, or do I have to explicitly convert the buffer to 32-bit, then pass it to QImage? Or else write my own function, porting this C++ example: