def save_figure(self, filename, format='png', size=None, magnification='auto', overwrite=False): r""" Method for saving the figure of the current `figure_id` to file. Parameters ---------- filename : `str` or `file`-like object The string path or file-like object to save the figure at/into. format : `str` The format to use. This must match the file path if the file path is a `str`. size : `tuple` of `int` or ``None``, optional The size of the image created (unless magnification is set, in which case it is the size of the window used for rendering). If ``None``, then the figure size is used. magnification : `double` or ``'auto'``, optional The magnification is the scaling between the pixels on the screen, and the pixels in the file saved. If you do not specify it, it will be calculated so that the file is saved with the specified size. If you specify a magnification, Mayavi will use the given size as a screen size, and the file size will be ``magnification * size``. If ``'auto'``, then the magnification will be set automatically. overwrite : `bool`, optional If ``True``, the file will be overwritten if it already exists. """ from menpo.io.output.base import _export savefig_args = {'size': size, 'figure': self.figure, 'magnification': magnification} # Use the export code so that we have a consistent interface _export(savefig_args, filename, self._extensions_map, format, overwrite=overwrite)
def save_figure(self, filename, format='png', size=None, magnification='auto', overwrite=False): r""" Method for saving the figure of the current `figure_id` to file. Parameters ---------- filename : `str` or `file`-like object The string path or file-like object to save the figure at/into. format : `str` The format to use. This must match the file path if the file path is a `str`. size : `tuple` of `int`, optional The size of the image created (unless magnification is set, in which case it is the size of the window used for rendering). magnification : `double`, optional The magnification is the scaling between the pixels on the screen, and the pixels in the file saved. If you do not specify it, it will be calculated so that the file is saved with the specified size. If you specify a magnification, Mayavi will use the given size as a screen size, and the file size will be ``magnification * size``. overwrite : `bool`, optional If ``True``, the file will be overwritten if it already exists. """ from menpo.io.output.base import _export savefig_args = {'size': size, 'figure': self.figure, 'magnification': magnification} # Use the export code so that we have a consistent interface _export(savefig_args, filename, self._extensions_map, format, overwrite=overwrite)
def export_textured_mesh(mesh, filepath, extension=None, texture_extension='jpg', overwrite=False): r""" Exports a given textured mesh. The ``filepath`` argument must be a string containing the filepath to write the mesh out to. Unlike the other export methods, this cannot take a file-like object because two files are written. If no ``extension`` is provided then the export type is calculated based on the filepath extension. The exported texture is always placed in the same directory as the exported mesh and is given the same base name. Parameters ---------- mesh : :map:`PointCloud` The mesh to export. filepath : `str` The string path to save the object at/into. extension : `str` or None, optional The extension to use for the exported mesh, this must match the file path if the file path is a string. Determines the type of exporter that is used for the mesh. texture_extension : `str`, optional Determines the type of exporter that is used for the texture. Default: 'jpg' overwrite : `bool`, optional Whether or not to overwrite a file if it already exists. Raises ------ ValueError File already exists and ``overwrite`` != ``True`` ValueError ``fp`` is a `str` and the ``extension`` is not ``None`` and the two extensions do not match ValueError The provided extension does not match to an existing exporter type (the output type is not supported). ValueError The mesh is not textured. """ if not hasattr(mesh, 'texture'): raise ValueError('Must supply a textured mesh.') _export(mesh, filepath, mesh_types, extension, overwrite) # Put the image next to the mesh image_output_path = Path(filepath).with_suffix( _normalise_extension(texture_extension)) _export(mesh.texture, str(image_output_path), image_types, texture_extension, overwrite)
def export_landmark_file(landmark_group, fp, extension=None, overwrite=False): r""" Exports a given landmark group. The ``fp`` argument can be either or a `str` or any Python type that acts like a file. If a file is provided, the ``extension`` kwarg **must** be provided. If no ``extension`` is provided and a `str` filepath is provided, then the export type is calculated based on the filepath extension. Due to the mix in string and file types, an explicit overwrite argument is used which is ``False`` by default. Parameters ---------- landmark_group : :map:`LandmarkGroup` The landmark group to export. fp : `str` or `file`-like object The string path or file-like object to save the object at/into. extension : `str` or None, optional The extension to use, this must match the file path if the file path is a string. Determines the type of exporter that is used. overwrite : `bool`, optional Whether or not to overwrite a file if it already exists. Raises ------ ValueError File already exists and ``overwrite`` != ``True`` ValueError ``fp`` is a `str` and the ``extension`` is not ``None`` and the two extensions do not match ValueError ``fp`` is a `file`-like object and ``extension`` is ``None`` ValueError The provided extension does not match to an existing exporter type (the output type is not supported). """ from .extensions import landmark_types _export(landmark_group, fp, landmark_types, extension, overwrite)
def export_landmark_file(landmark_group, fp, extension=None, overwrite=False): r""" Exports a given landmark group. The ``fp`` argument can be either or a `str` or any Python type that acts like a file. If a file is provided, the ``extension`` kwarg **must** be provided. If no ``extension`` is provided and a `str` filepath is provided, then the export type is calculated based on the filepath extension. Due to the mix in string and file types, an explicit overwrite argument is used which is ``False`` by default. Parameters ---------- landmark_group : :map:`LandmarkGroup` The landmark group to export. fp : `str` or `file`-like object The string path or file-like object to save the object at/into. extension : `str` or None, optional The extension to use, this must match the file path if the file path is a string. Determines the type of exporter that is used. overwrite : `bool`, optional Whether or not to overwrite a file if it already exists. Raises ------ ValueError File already exists and ``overwrite`` != ``True`` ValueError ``fp`` is a `str` and the ``extension`` is not ``None`` and the two extensions do not match ValueError ``fp`` is a `file`-like object and ``extension`` is ``None`` ValueError The provided extension does not match to an existing exporter type (the output type is not supported). """ _export(landmark_group, fp, landmark_types, extension, overwrite)
def save_figure(self, filename, format='png', dpi=None, face_colour='w', edge_colour='w', orientation='portrait', paper_type='letter', transparent=False, pad_inches=0.1, overwrite=False): r""" Method for saving the figure of the current `figure_id` to file. Parameters ---------- filename : `str` or `file`-like object The string path or file-like object to save the figure at/into. format : `str` The format to use. This must match the file path if the file path is a `str`. dpi : `int` > 0 or ``None``, optional The resolution in dots per inch. face_colour : See Below, optional The face colour of the figure rectangle. Example options :: {``r``, ``g``, ``b``, ``c``, ``m``, ``k``, ``w``} or ``(3, )`` `ndarray` or `list` of len 3 edge_colour : See Below, optional The edge colour of the figure rectangle. Example options :: {``r``, ``g``, ``b``, ``c``, ``m``, ``k``, ``w``} or ``(3, )`` `ndarray` or `list` of len 3 orientation : {``portrait``, ``landscape``}, optional The page orientation. paper_type : See Below, optional The type of the paper. Example options :: {``letter``, ``legal``, ``executive``, ``ledger``, ``a0`` through ``a10``, ``b0` through ``b10``} transparent : `bool`, optional If ``True``, the axes patches will all be transparent; the figure patch will also be transparent unless `face_colour` and/or `edge_colour` are specified. This is useful, for example, for displaying a plot on top of a coloured background on a web page. The transparency of these patches will be restored to their original values upon exit of this function. pad_inches : `float`, optional Amount of padding around the figure. overwrite : `bool`, optional If ``True``, the file will be overwritten if it already exists. """ from menpo.io.output.base import _export save_fig_args = {'dpi': dpi, 'facecolour': face_colour, 'edgecolour': edge_colour, 'orientation': orientation, 'papertype': paper_type, 'format': format, 'transparent': transparent, 'pad_inches': pad_inches, 'bbox_inches': 'tight', 'frameon': None} # Use the export code so that we have a consistent interface _export(save_fig_args, filename, self._extensions_map, format, overwrite=overwrite)
def export_mesh(mesh, fp, extension=None, overwrite=False, **kwargs): r""" Exports a given mesh. The ``fp`` argument can be either a `str` or any Python type that acts like a file. If a file is provided, the ``extension`` kwarg **must** be provided. If no ``extension`` is provided and a `str` filepath is provided, then the export type is calculated based on the filepath extension. Due to the mix of string and file types, an explicit overwrite argument is used which is ``False`` by default. Parameters ---------- mesh : :map:`PointCloud` The mesh to export. fp : `str` or `file`-like object The string path or file-like object to save the object at/into. extension : `str` or None, optional The extension to use, this must match the file path if the file path is a string. Determines the type of exporter that is used. overwrite : `bool`, optional Whether or not to overwrite a file if it already exists. **kwargs : `dict` Keyword arguments to be passed through to exporter function. Raises ------ ValueError File already exists and ``overwrite`` != ``True`` ValueError ``fp`` is a `str` and the ``extension`` is not ``None`` and the two extensions do not match ValueError ``fp`` is a `file`-like object and ``extension`` is ``None`` ValueError The provided extension does not match to an existing exporter type (the output type is not supported). """ from .extensions import mesh_types, mesh_types_paths_only if isinstance(fp, str): fp = Path(fp) if isinstance(fp, Path): _, extension = _validate_and_get_export_func(fp, mesh_types, extension, overwrite, return_extension=True) if extension in mesh_types_paths_only: _export_paths_only(mesh, fp, mesh_types, extension, overwrite, exporter_kwargs=kwargs) else: _export(mesh, fp, mesh_types, extension, overwrite, exporter_kwargs=kwargs)
def export_textured_mesh(mesh, file_path, extension=None, texture_extension=".jpg", overwrite=False, **kwargs): r""" Exports a given textured mesh. The ``filepath`` argument must be a string containing the filepath to write the mesh out to. Unlike the other export methods, this cannot take a file-like object because two files are written. If no ``extension`` is provided then the export type is calculated based on the filepath extension. The exported texture is always placed in the same directory as the exported mesh and is given the same base name. Parameters ---------- mesh : :map:`PointCloud` The mesh to export. file_path : `pathlib.Path` or `str` The path to save the object at/into. extension : `str` or None, optional The extension to use for the exported mesh, this must match the file path if the file path is a string. Determines the type of exporter that is used for the mesh. texture_extension : `str`, optional Determines the type of exporter that is used for the texture. overwrite : `bool`, optional Whether or not to overwrite a file if it already exists. **kwargs : `dict` Keyword arguments to be passed through to exporter function. Raises ------ ValueError File already exists and ``overwrite`` != ``True`` ValueError ``fp`` is a `str` and the ``extension`` is not ``None`` and the two extensions do not match ValueError The provided extension does not match to an existing exporter type (the output type is not supported). ValueError The mesh is not textured. """ from menpo.shape import TexturedTriMesh if not isinstance(mesh, TexturedTriMesh): raise ValueError("Must supply a textured mesh.") file_path = _enforce_only_paths_supported(file_path, "textured mesh") export_mesh( mesh, file_path, extension=extension, overwrite=overwrite, exporter_kwargs=kwargs, ) # Put the image next to the mesh texture_extension = _normalize_extension(texture_extension) image_output_path = Path(file_path).with_suffix(texture_extension) _export(mesh.texture, image_output_path, image_types, texture_extension, overwrite)