Blank image with vtkImageMapToColors, works in vtk8

This code, based on the ImageMapToColors example, does what I want in vtk8, but it produces a blank image on vtk9.

width = 15
height = 15
data = np.arange(width * height, dtype='float32').reshape((width, height))

data_importer = vtk.vtkImageImport()
data_str = data.tostring()
data_importer.SetDataSpacing(1.0, 1.0, 1.0)
data_importer.SetDataOrigin(0.0, 0.0, 0.0)
data_importer.SetWholeExtent(0, width - 1, 0, height - 1, 0, 0)
data_importer.SetDataExtentToWholeExtent()
data_importer.SetDataScalarTypeToFloat()
data_importer.SetNumberOfScalarComponents(1)
data_importer.CopyImportVoidPointer(data_str, len(data_str))
data_importer.Update()

lut = vtk.vtkLookupTable()
lut.SetNumberOfTableValues(256)
lut.SetRange(0.0, 255.0)
lut.Build()

color = vtk.vtkImageMapToColors()
color.SetLookupTable(lut)
color.PassAlphaToOutputOn()
color.SetInputConnection(data_importer.GetOutputPort())

actor = vtk.vtkImageActor()
actor.SetInterpolate(False)
actor.GetMapper().SetInputConnection(color.GetOutputPort())

renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.ResetCamera()

render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(render_window)
iren.Initialize()
iren.Start()

The image is ok in vtk9 only when I remove the vtkLookupTable and vtkImageMapToColors parts, but I need to use a LUT for the application I’m trying to code. What do I need to modify to have a working image in vtk9?

Thanks for including the code. However, it works fine for me with VTK 9. The image is red at the bottom left, progressing to blue at the top right.

Can you provide more information about your system and about the VTK package that you are using? I ran your test on linux (ubuntu 20.04), with a VTK 9 that I built from source against the system libraries (i.e. not a pip or conda package).

My system is not super clean! I ran both versions in VMware, with WMware tools installed, vtk installed with pip.

  1. Mint 19.3 Python 3.6 vtk8 ✓
  2. Mint 19.3 Python 3.6 vtk9 :x:
  3. Mint 20.0 Python 3.8 vtk9 :x:

However, I asked my colleagues who have a real linux installation (not a vm) to test it for me.

  1. Mint 19.2 Python 3.7 vtk9 (pip) ✓
  2. Archlinux Python 3.8 vtk9 (apt) ✓

It looks like there’s something wrong with my VM. Are there some known problems to using a vm with vtk9? I’ve been using this code in a vm for years without any troubles so I’m quite surprised!

Do you know which OpenGL backend was used to build your vtk8 package? It might be incompatible with vmware’s GL.

If it uses the old backend, then this will succeed:

import vtk.vtkRenderingOpenGL

For the new backend, the module’s name is vtkRenderingOpenGL2.

ModuleNotFoundError: No module named 'vtk.vtkRenderingOpenGL'
But vtk.vtkRenderingOpenGL2 can be imported without trouble (still got a blank image)

I tried building vtk 9.0.1 + Python wrapping myself, everything went well, I deleted the pip version, I added ~/VTK-9.0.1/_build/lib/python3.8/site-packages to my PYTHONPATH and run the program … I got the same blank image again :frowning:

Unless you have other ideas, I’ll simply code my project on a non-vm environment.

Well, at least now we know that the OpenGL2 backend isn’t completely incompatible with vmware. Rather, the issue occurred within the OpenGL2 backend sometime between VTK 8 and VTK 9. Unfortunately it would take some detective work to identify exactly what change caused the problem.

Hopefully someone out there who has experience with VTK9 + vmware can provide some insight?