def getter(self): lines = getter_getter(self)() length = lines.GetNumberOfCells() if length: arr = vtk_to_numpy(lines.GetData()) return unpack_lengths(arr) else: return []
def points(self): points = self.vtk_polydata.GetPoints() if points is None: return None else: data = points.GetData() self._temp.append(data) return vtk_to_numpy(data)
def write(arr, path, format=None, quality=95): """Write an image from a file using one of VTK's ``vtkFormatWriter`` classes where ``Format`` is replaced by JPEG or PNG. :param arr: **arr** can be a `vtkImageData`_ or a numpy array. :type arr: np.ndarray :param path: File path to write to. :type path: str, os.Pathlike, io.BytesIO, :param format: Image format extension (e.g. jpg), not needed if format can be determined from **path**, defaults to ``None``. :type format: str, optional :param quality: Lossy compression quality, only applicable to JPEGs, defaults to 95. :type quality: int from 0 to 100, optional :return: The raw image binary if ``path is None``, ``NotImplemented`` if the filetype is unknown. Otherwise no return value. :rtype: bytes See :meth:`read` for more information. .. note:: BytesIO and raw bytes functionality is new in vtkplotlib >= 1.3.0. Older versions are hardcoded to write to disk and therefore **path** must be a filename and not a BytesIO or similar pseudo file object. """ format = _normalise_format(path, format) try: Writer = getattr(vtk, "vtk{}Writer".format(format)) except AttributeError: return NotImplemented im_data = as_vtkimagedata(arr) im_data.Modified() writer = Writer() writer.SetInputDataObject(im_data) if Writer is vtk.vtkJPEGWriter: writer.SetQuality(quality) if nuts_and_bolts.isinstance_PathLike(path): with PathHandler(path, "w") as path_handler: writer.SetFileName(path_handler.access_path) writer.Update() writer.Write() return writer.WriteToMemoryOn() writer.Update() writer.Write() binary = vtk_to_numpy(writer.GetResult()).tobytes() if path is None: return binary path.write(binary)
def vtkimagedata_to_array(image_data): """Convert a vtkImageData to numpy array. .. seealso:: :meth:`vtkimagedata_from_array` for the reverse. """ points = vtk_to_numpy(image_data.GetPointData().GetScalars()) shape = image_data.GetDimensions() # Be careful here. shape[2] isn't the number of values per pixel as you'd # expect. Rather it is a 3rd dimension as vtkImageData is # supposed to hold volumes. `image_data.GetNumberOfScalarComponents()` gets # the right value (usually 3 for RGB). # Additionally vtk uses cartesian coordinates in images which isn't the # norm - hence the axes swapping and mirroring. shape = (shape[1], shape[0], image_data.GetNumberOfScalarComponents()) return points.reshape(shape)[::-1]
def getter(self): colors = getter_getter(self)().GetScalars() if colors is None: return return vtk_to_numpy(colors)