vtkImageMapper + vtkActor2D show up as black

Hi all,

I’m struggling with getting a vtkImageMapper + vtkActor2D to show up correctly. It’s showing up in black when I expect it to be all red.

Test Case

#!/usr/bin/env python

from vtk import (
    vtkImageData,
    vtkImageMapper,
    vtkActor2D,
    vtkRenderer,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    VTK_FLOAT,
)


def main():
    # Create a 100x100 red image
    width = 100
    height = 100

    imageData = vtkImageData()
    imageData.SetSpacing(1.0, 1.0, 1.0)
    imageData.SetOrigin(0.0, 0.0, 0.0)
    imageData.SetDimensions(width, height, 1)
    imageData.AllocateScalars(VTK_FLOAT, 3)

    for x in range(width):
        for y in range(height):
            imageData.SetScalarComponentFromFloat(x, y, 0, 0, 255) # R
            imageData.SetScalarComponentFromFloat(x, y, 0, 1, 0)   # G
            imageData.SetScalarComponentFromFloat(x, y, 0, 2, 0)   # B

    # Create mapper and actor
    mapper = vtkImageMapper()
    mapper.SetInputData(imageData)

    actor = vtkActor2D()
    actor.SetMapper(mapper)

    # Create renderer/window/interactor
    renderer = vtkRenderer()
    renderer.SetBackground(1.0, 1.0, 1.0)
    renderer.AddActor(actor)

    window = vtkRenderWindow()
    window.AddRenderer(renderer)

    interactor = vtkRenderWindowInteractor()
    interactor.SetRenderWindow(window)
    interactor.Start()


if __name__ == '__main__':
    main()

Result

black_image

What’s puzzling is that I’m doing something very similar in a C++ application and it works well. But I’m constructing the image a little differently there (using vtkQImageToImageData).

Am I doing something wrong when I manually create a red vtkImageData like this, by setting the first component of its scalars to 255 using SetScalarComponentFromFloat? I’ve tried changing the type of the scalars to VTK_UNSIGNED_CHAR but it didn’t change anything. I have a feeling I’m doing something basic wrong.

I’m using VTK 8.1.2 from PyPI.

I found the issue, I had to set the window/level of the mapper using:

    mapper.SetColorWindow(255.0)
    mapper.SetColorLevel(127.5)