I’m trying to take a screenshot (vtkWindowToImageFilter
) of a 4x4 toy image with a vtkLookupTable
that transform NaN values into transparent values. Here’s my code:
import numpy as np
import vtk
data = np.array(
[
[0.0, 20.0, 0.5, 10.0],
[10.0, 0.0, 10.5, 0.5],
[0.5, 10.5, 0.0, 10.0],
[10.0, 0.5, 20.0, np.NAN],
],
dtype="float32",
)
data_importer = vtk.vtkImageImport()
data_str = data.tobytes()
data_importer.CopyImportVoidPointer(data_str, len(data_str))
data_importer.SetDataScalarTypeToFloat()
data_importer.SetNumberOfScalarComponents(1)
data_importer.SetDataSpacing(1.0, 1.0, 0.0)
data_importer.SetDataOrigin(0.5, 0.5, 0.5)
data_importer.SetWholeExtent(0, data.shape[1] - 1, 0, data.shape[0] - 1, 0, 0)
data_importer.SetDataExtentToWholeExtent()
data_importer.Update()
lut = vtk.vtkLookupTable()
lut.SetRange(0.0, 20.0)
lut.SetNanColor(1.0, 0.0, 0.0, 0.0)
lut.SetRampToLinear()
lut.SetHueRange(0.0, 0.0)
lut.SetSaturationRange(0.0, 0.0)
lut.SetValueRange(0.0, 1.0)
color = vtk.vtkImageMapToColors()
color.SetLookupTable(lut)
color.SetOutputFormatToRGB()
color.SetInputConnection(data_importer.GetOutputPort())
actor = vtk.vtkImageActor()
actor.SetInterpolate(False)
actor.GetMapper().SetInputConnection(color.GetOutputPort())
actor.GetMapper().BorderOn()
actor.GetProperty().SetOpacity(1.0)
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.ResetCamera()
renderer.SetBackground(1.0, 0.0, 0.0)
render_window = vtk.vtkRenderWindow()
render_window.SetSize(800, 600)
render_window.AddRenderer(renderer)
render_window.SetAlphaBitPlanes(1)
w2if = vtk.vtkWindowToImageFilter()
w2if.SetInput(render_window)
w2if.SetInputBufferTypeToRGBA()
w2if.Update()
writer = vtk.vtkPNGWriter()
writer.SetFileName("test.png")
writer.SetInputData(w2if.GetOutput())
writer.Write()
This was working perfectly on v8.1.2. Both the background and the NaN pixel were transparent. In order to work, I had to use color.SetOutputFormatToRGBA()
.
On v9.2.2 however, the NaN pixel gets a different treatment, wathever method I use.
- If I use
SetOutputFormatToRGBA
, the whole image is transparent. - If I use
SetOutputFormatToRGB
, I get the image below