convert

TemporaryDirectory([suffix, prefix, dir])

Create and return a temporary directory.

load_image(filename[, as_vtktype, use_pillow])

Load an image.

matplotlib_figure_to_numpy(fig[, dpi, …])

Convert a Matplotlib figure to a 3D numpy array with RGBA channels.

TemporaryDirectory

class fury.convert.TemporaryDirectory(suffix=None, prefix=None, dir=None)[source]

Bases: object

Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager. For .. rubric:: Example

with TemporaryDirectory() as tmpdir:

Upon exiting the context, the directory and everything contained in it are removed.

__init__(suffix=None, prefix=None, dir=None)[source]

Initialize self. See help(type(self)) for accurate signature.

cleanup()[source]

load_image

fury.convert.load_image(filename, as_vtktype=False, use_pillow=True)[source]

Load an image.

Parameters
  • filename (str) – should be png, bmp, jpeg or jpg files

  • as_vtktype (bool, optional) – if True, return vtk output otherwise an ndarray. Default False.

  • use_pillow (bool, optional) – Use pillow python library to load the files. Default True

Returns

image – desired image array

Return type

ndarray or vtk output

matplotlib_figure_to_numpy

fury.convert.matplotlib_figure_to_numpy(fig, dpi=100, fname=None, flip_up_down=True, transparent=False)[source]

Convert a Matplotlib figure to a 3D numpy array with RGBA channels.

Parameters
  • fig (obj) – A matplotlib figure object

  • dpi (int, optional) – Dots per inch

  • fname (str, optional) – If fname is given then the array will be saved as a png to this position.

  • flip_up_down (bool, optional) – The origin is different from matlplotlib default and VTK’s default behaviour (default True).

  • transparent (bool, optional) – Make background transparent (default False).

Returns

arr – a numpy 3D array of RGBA values

Return type

ndarray

Notes

The safest way to read the pixel values from the figure was to save them using savefig as a png and then read again the png. There is a cleaner way found here http://www.icare.univ-lille1.fr/drupal/node/1141 where you can actually use fig.canvas.tostring_argb() to get the values directly without saving to the disk. However, this was not stable across different machines and needed more investigation from what time permited.