Hi,
I have a volume and I Slice this volume. This slice, called slice1 is coloured
using a ColorTransferFunction and an OpacityTransferFunction. This works well
and when I use Show() and Render() it shows up fine.
Now I want to access these RGBA values so that I can add that colour value to
an image I have prepared using the PIL library. The problem is that no matter
where I look I am unable to find any access point. No arrays I can access and
no functions I can call. The closest thing I have found is this: array =
slice1.GetPointData().GetArray("u150-ref0"). This returns an array according,
but I am not able to access it.
dir(array) shows that there is no __getItem__ function and I couldn't find any
other access method. I went into the source code of the Paraview python modules
and I found a function called paraview.numpy_support.vtk_to_numpy(), but this
would not work since the objects I tried feeding it did not have __buffer__ at
some level.
Please refer to the source file I have attached for the full code. At the very
bottom there is a double for loop, within which I want to do the extraction of
the coloured pixels.
Here is a link to an image showing a single slice rendered:
http://puu.sh/rAijX/ba71c4b7d9.png
Thank you,
Magnus Elden
# state file generated using paraview version 5.1.2
# ----------------------------------------------------------------
# setup views used in the visualization
# ----------------------------------------------------------------
#### import the simple module from the paraview
from paraview.simple import *
from time import sleep
from PIL import Image
import math
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()
# Create a new 'Render View'
renderView1 = CreateView('RenderView')
renderView1.ViewSize = [1108, 853]
renderView1.AxesGrid = 'GridAxes3DActor'
renderView1.CenterOfRotation = [83.0539321899414, 68.33893013000488,
81.69787979125977]
renderView1.StereoType = 0
renderView1.CameraPosition = [0, 0, 200]
renderView1.CameraFocalPoint = [80.28337733924155, 34.389806037434816,
87.3043883633431]
renderView1.CameraViewUp = [0.3784900662153272, -0.18080221906603647,
-0.9077752075029998]
renderView1.CameraParallelScale = 29.414632087040744
renderView1.Background = [0.32, 0.34, 0.43]
# ----------------------------------------------------------------
# setup the data processing pipelines
# ----------------------------------------------------------------
# create a new 'XDMF Reader'
u150ref0xdmf =
XDMFReader(FileNames=['C:\\Users\\NoobsDeSroobs\\Downloads\\data\\volumes\\u150-ref0\\u150-ref0.xdmf'])
u150ref0xdmf.PointArrayStatus = ['u150-ref0']
u150ref0xdmf.GridStatus = ['u150-ref0_0', 'u150-ref0_1', 'u150-ref0_2',
'u150-ref0_3', 'u150-ref0_4', 'u150-ref0_5', 'u150-ref0_6',
'u150-ref0_7', 'u150-ref0_8', 'u150-ref0_9',
'u150-ref0_10', 'u150-ref0_11', 'u150-ref0_12', 'u150-ref0_13', 'u150-ref0_14',
'u150-ref0_15', 'u150-ref0_16', 'u150-ref0_17']
u150ref0xdmf.UpdatePipeline()
data = u150ref0xdmf.GetDataInformation()
bounds = data.DataInformation.GetBounds()
# create a new 'Slice'
slice1 = Slice(Input=u150ref0xdmf)
slice1.SliceType = 'Plane'
slice1.SliceOffsetValues = [0.0]
numSlices = 100.0
numCols = math.ceil(math.sqrt(numSlices))
numRows = math.ceil(numSlices/numCols)
sliceDiff = (bounds[5] - bounds[4])/numSlices
volXSize = bounds[1] - bounds[0]
volYSize = bounds[3] - bounds[2]
volXCellSize = volXSize / numCols
volYCellSize = volYSize / numRows
for i in range(0, numSlices):
image = Image.new("RGBA", (volXSize, volYSize));
imageSize = image.size
pixels = image.load();
# init the 'Plane' selected for 'SliceType'
slice1.SliceType.Origin = [83.0539321899414, 68.3389301300049, bounds[4] +
(i*sliceDiff)]
slice1.SliceType.Normal = [0.0, 0.0, 1.0]
# ----------------------------------------------------------------
# setup color maps and opacity mapes used in the visualization
# note: the Get..() functions create a new object, if needed
# ----------------------------------------------------------------
# get color transfer function/color map for 'u150ref0'
u150ref0LUT = GetColorTransferFunction('u150ref0')
u150ref0LUT.RGBPoints = [0.0, 0.231373, 0.298039, 0.752941,
271.4392904696593, 0.865003, 0.865003, 0.865003,
542.8785809393186, 0.705882, 0.0156863, 0.14902]
u150ref0LUT.ScalarRangeInitialized = 1.0
# get opacity transfer function/opacity map for 'u150ref0'
u150ref0PWF = GetOpacityTransferFunction('u150ref0')
u150ref0PWF.Points = [0.0, 0.0, 0.5, 0.0, 542.8785809393186, 1.0, 0.5, 0.0]
u150ref0PWF.ScalarRangeInitialized = 1
# ----------------------------------------------------------------
# setup the visualization in view 'renderView1'
# ----------------------------------------------------------------
# show data from slice1
slice1Display = Show(slice1, renderView1)
# trace defaults for the display properties.
slice1Display.ColorArrayName = ['POINTS', 'u150-ref0']
slice1Display.LookupTable = u150ref0LUT
slice1Display.OSPRayScaleArray = 'u150-ref0'
slice1Display.OSPRayScaleFunction = 'PiecewiseFunction'
slice1Display.GlyphType = 'Arrow'
slice1Display.SetScaleArray = [None, '']
slice1Display.ScaleTransferFunction = 'PiecewiseFunction'
slice1Display.OpacityArray = [None, '']
slice1Display.OpacityTransferFunction = 'PiecewiseFunction'
# show color legend
slice1Display.SetScalarBarVisibility(renderView1, True)
# setup the color legend parameters for each legend in this view
# get color legend/bar for u150ref0LUT in view renderView1
u150ref0LUTColorBar = GetScalarBar(u150ref0LUT, renderView1)
u150ref0LUTColorBar.Title = 'u150-ref0'
u150ref0LUTColorBar.ComponentTitle = 'Magnitude'
# ----------------------------------------------------------------
# finally, restore active source
SetActiveSource(slice1)
# ----------------------------------------------------------------
#Save the slice to a PNG image
sliceBounds = slice1.GetDataInformation().DataInformation.GetBounds()
for y in range(0, volXSize):
for x in range(0, volYSize):
currCellX = sliceBounds[0] + x
currCellY = sliceBounds[2] + y
currCellZ = sliceBounds[4]
#Get the RGBA value somehow....
pointValue = slice1.PointData[currCellX, currCellY, currCellZ]
pixels[x, y] = pointValue
Render()
sleep(1)
_______________________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
Please keep messages on-topic and check the ParaView Wiki at:
http://paraview.org/Wiki/ParaView
Search the list archives at: http://markmail.org/search/?q=ParaView
Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/paraview