def save(self, filename, binary=True): """Save this vtk object to file. Parameters ---------- filename : str Filename of output file. Writer type is inferred from the extension of the filename. binary : bool, optional If True, write as binary, else ASCII. Notes ----- Binary files write much faster than ASCII and have a smaller file size. """ if self._WRITERS is None: raise NotImplementedError('{} writers are not specified, this should be a' \ ' dict of (file extension: vtkWriter type)' .format(self.__class__.__name__)) filename = os.path.abspath(os.path.expanduser(filename)) file_ext = fileio.get_ext(filename) if file_ext not in self._WRITERS: raise ValueError('Invalid file extension for this data type. Must be one of: {}'.format( self._WRITERS.keys())) writer = self._WRITERS[file_ext]() fileio.set_vtkwriter_mode(vtk_writer=writer, use_binary=binary) writer.SetFileName(filename) writer.SetInputData(self) writer.Write()
def save(self, filename, binary=True): """Save this vtk object to file. Parameters ---------- filename : str, pathlib.Path Filename of output file. Writer type is inferred from the extension of the filename. binary : bool, optional If True, write as binary, else ASCII. Notes ----- Binary files write much faster than ASCII and have a smaller file size. """ if self._WRITERS is None: raise NotImplementedError( f'{self.__class__.__name__} writers are not specified,' ' this should be a dict of (file extension: vtkWriter type)') file_path = Path(filename).resolve() file_ext = file_path.suffix if file_ext not in self._WRITERS: raise ValueError('Invalid file extension for this data type.' f' Must be one of: {self._WRITERS.keys()}') writer = self._WRITERS[file_ext]() fileio.set_vtkwriter_mode(vtk_writer=writer, use_binary=binary) writer.SetFileName(str(file_path)) writer.SetInputData(self) writer.Write()
def save(self, filename: str, binary=True, texture=None): """Save this vtk object to file. Parameters ---------- filename : str, pathlib.Path Filename of output file. Writer type is inferred from the extension of the filename. binary : bool, optional If ``True``, write as binary. Otherwise, write as ASCII. texture : str, np.ndarray, optional Write a single texture array to file when using a PLY file. Texture array must be a 3 or 4 component array with the datatype ``np.uint8``. Array may be a cell array or a point array, and may also be a string if the array already exists in the PolyData. If a string is provided, the texture array will be saved to disk as that name. If an array is provided, the texture array will be saved as ``'RGBA'`` .. note:: This feature is only available when saving PLY files. Notes ----- Binary files write much faster than ASCII and have a smaller file size. """ if self._WRITERS is None: raise NotImplementedError( f'{self.__class__.__name__} writers are not specified,' ' this should be a dict of (file extension: vtkWriter type)') file_path = Path(filename) file_path = file_path.expanduser() file_path = file_path.resolve() file_ext = file_path.suffix if file_ext not in self._WRITERS: raise ValueError('Invalid file extension for this data type.' f' Must be one of: {self._WRITERS.keys()}') writer = self._WRITERS[file_ext]() fileio.set_vtkwriter_mode(vtk_writer=writer, use_binary=binary) try: writer.SetDataModeToAppended() writer.SetCompressorTypeToLZ4() writer.SetCompressionLevel(9) except: pass writer.SetFileName(str(file_path)) writer.SetInputData(self) if file_ext == '.ply' and texture is not None: if isinstance(texture, str): writer.SetArrayName(texture) array_name = texture elif isinstance(texture, np.ndarray): array_name = '_color_array' self[array_name] = texture writer.SetArrayName(array_name) # enable alpha channel if applicable if self[array_name].shape[-1] == 4: # type: ignore writer.SetEnableAlpha(True) writer.Write()