/usr/lib/python2.7/dist-packages/vtk/util/vtkImageImportFromArray.py is in python-vtk6 6.3.0+dfsg1-11build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | """
vtkImageImportFromArray: a NumPy front-end to vtkImageImport
Load a python array into a vtk image.
To use this class, you must have NumPy installed (http://numpy.scipy.org/)
Methods:
SetArray() -- set the numpy array to load
Update() -- generate the output
GetOutput() -- get the image as vtkImageData
GetOutputPort() -- connect to VTK pipeline
Methods from vtkImageImport:
(if you don't set these, sensible defaults will be used)
SetDataExtent()
SetDataSpacing()
SetDataOrigin()
"""
from vtk import vtkImageImport
from vtk import VTK_SIGNED_CHAR
from vtk import VTK_UNSIGNED_CHAR
from vtk import VTK_SHORT
from vtk import VTK_UNSIGNED_SHORT
from vtk import VTK_INT
from vtk import VTK_UNSIGNED_INT
from vtk import VTK_LONG
from vtk import VTK_UNSIGNED_LONG
from vtk import VTK_FLOAT
from vtk import VTK_DOUBLE
class vtkImageImportFromArray:
def __init__(self):
self.__import = vtkImageImport()
self.__ConvertIntToUnsignedShort = False
self.__Array = None
# type dictionary: note that python doesn't support
# unsigned integers properly!
__typeDict = {'b':VTK_SIGNED_CHAR, # int8
'B':VTK_UNSIGNED_CHAR, # uint8
'h':VTK_SHORT, # int16
'H':VTK_UNSIGNED_SHORT, # uint16
'i':VTK_INT, # int32
'I':VTK_UNSIGNED_INT, # uint32
'f':VTK_FLOAT, # float32
'd':VTK_DOUBLE, # float64
'F':VTK_FLOAT, # float32
'D':VTK_DOUBLE, # float64
}
__sizeDict = { VTK_SIGNED_CHAR:1,
VTK_UNSIGNED_CHAR:1,
VTK_SHORT:2,
VTK_UNSIGNED_SHORT:2,
VTK_INT:4,
VTK_UNSIGNED_INT:4,
VTK_FLOAT:4,
VTK_DOUBLE:8 }
# convert 'Int32' to 'unsigned short'
def SetConvertIntToUnsignedShort(self,yesno):
self.__ConvertIntToUnsignedShort = yesno
def GetConvertIntToUnsignedShort(self):
return self.__ConvertIntToUnsignedShort
def ConvertIntToUnsignedShortOn(self):
self.__ConvertIntToUnsignedShort = True
def ConvertIntToUnsignedShortOff(self):
self.__ConvertIntToUnsignedShort = False
def Update(self):
self.__import.Update()
# get the output
def GetOutputPort(self):
return self.__import.GetOutputPort()
# get the output
def GetOutput(self):
return self.__import.GetOutput()
# import an array
def SetArray(self,imArray):
self.__Array = imArray
numComponents = 1
dim = imArray.shape
if len(dim) == 0:
dim = (1,1,1)
elif len(dim) == 1:
dim = (1, 1, dim[0])
elif len(dim) == 2:
dim = (1, dim[0], dim[1])
elif len(dim) == 4:
numComponents = dim[3]
dim = (dim[0],dim[1],dim[2])
typecode = imArray.dtype.char
ar_type = self.__typeDict[typecode]
complexComponents = 1
if (typecode == 'F' or typecode == 'D'):
numComponents = numComponents * 2
complexComponents = 2
if (self.__ConvertIntToUnsignedShort and typecode == 'i'):
imArray = imArray.astype('h')
ar_type = VTK_UNSIGNED_SHORT
size = len(imArray.flat)*self.__sizeDict[ar_type]*complexComponents
self.__import.CopyImportVoidPointer(imArray, size)
self.__import.SetDataScalarType(ar_type)
self.__import.SetNumberOfScalarComponents(numComponents)
extent = self.__import.GetDataExtent()
self.__import.SetDataExtent(extent[0],extent[0]+dim[2]-1,
extent[2],extent[2]+dim[1]-1,
extent[4],extent[4]+dim[0]-1)
self.__import.SetWholeExtent(extent[0],extent[0]+dim[2]-1,
extent[2],extent[2]+dim[1]-1,
extent[4],extent[4]+dim[0]-1)
def GetArray(self):
return self.__Array
# a whole bunch of methods copied from vtkImageImport
def SetDataExtent(self,extent):
self.__import.SetDataExtent(extent)
def GetDataExtent(self):
return self.__import.GetDataExtent()
def SetDataSpacing(self,spacing):
self.__import.SetDataSpacing(spacing)
def GetDataSpacing(self):
return self.__import.GetDataSpacing()
def SetDataOrigin(self,origin):
self.__import.SetDataOrigin(origin)
def GetDataOrigin(self):
return self.__import.GetDataOrigin()
|