vtkImageImportFromArray.py 4.8 KB

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