How to convert vtkData to RGB data?

Hi,all.
Here is one red slice of my image data(.vtk format)

I get this slice with the following code,

volume_node = slicer.util.getNode("vtkMRMLScalarVolumeNode1")
image = slicer.util.arrayFromVolume(volume_node)
red_slice = image[50,:,:]

However,its voxels are raw values which is not in [0,255] range.If I take a screenshot on this red slice and get a .png format picture,its pixels can convert into range [0,255].
I want to know if there is a way to convert the slice with the raw data.
This pseudocode is used to illustrate the idea.

red_slice = image[50,:,:]  # image is raw data, voxels are not in [0,255]
gray_red_slice = convert_to_gray_image(image[50,:,:])  # some function/methods to convert the intensity values

Thanks in advance for your help and advice!

You could transform the values into the [0,255] range by shifting and scaling them:

a = red_slice.min()
b = red_slice.max()
gray_red_slice = 255 * (red_slice-a) / (b-a)
2 Likes