yt.visualization.image_writer module

yt.visualization.image_writer.apply_colormap(image, color_bounds=None, cmap_name=None, func=<function <lambda>>)[source]

Apply a colormap to a floating point image, scaling to uint8.

This function will scale an image and directly call libpng to write out a colormapped version of that image. It is designed for rapid-fire saving of image buffers generated using yt.visualization.api.FixedResolutionBuffers and the likes.

Parameters:
  • image (array_like) – This is an (unscaled) array of floating point values, shape (N,N,) to save in a PNG file.

  • color_bounds (tuple of floats, optional) – The min and max to scale between. Outlying values will be clipped.

  • cmap_name (string, optional) – An acceptable colormap. See either yt.visualization.color_maps or https://scipy-cookbook.readthedocs.io/items/Matplotlib_Show_colormaps.html .

  • func (function, optional) – A function to transform the buffer before applying a colormap.

Returns:

to_plot

Return type:

uint8 image with colorbar applied.

yt.visualization.image_writer.display_in_notebook(image, max_val=None)[source]

A helper function to display images in an IPython notebook

Must be run from within an IPython notebook, or else it will raise a YTNotInsideNotebook exception.

Parameters:
  • image (array_like) – This is an (unscaled) array of floating point values, shape (N,N,3) or (N,N,4) to display in the notebook. The first three channels will be scaled automatically.

  • max_val (float, optional) – The upper limit to clip values of the image. Only applies to the first three channels.

yt.visualization.image_writer.map_to_colors(buff, cmap_name)[source]
yt.visualization.image_writer.multi_image_composite(fn, red_channel, blue_channel, green_channel=None, alpha_channel=None)[source]

Write an image with different color channels corresponding to different quantities.

Accepts at least a red and a blue array, of shape (N,N) each, that are optionally scaled and composited into a final image, written into fn. Can also accept green and alpha.

Parameters:
  • fn (string) – Filename to save

  • red_channel (array_like or tuple of image info) – Array, of shape (N,N), to be written into the red channel of the output image. If not already uint8, will be converted (and scaled) into uint8. Optionally, you can also specify a tuple that includes scaling information, in the form of (array_to_plot, min_value_to_scale, max_value_to_scale).

  • blue_channel (array_like or tuple of image info) – Array, of shape (N,N), to be written into the blue channel of the output image. If not already uint8, will be converted (and scaled) into uint8. Optionally, you can also specify a tuple that includes scaling information, in the form of (array_to_plot, min_value_to_scale, max_value_to_scale).

  • green_channel (array_like or tuple of image info, optional) – Array, of shape (N,N), to be written into the green channel of the output image. If not already uint8, will be converted (and scaled) into uint8. If not supplied, will be left empty. Optionally, you can also specify a tuple that includes scaling information, in the form of (array_to_plot, min_value_to_scale, max_value_to_scale).

  • alpha_channel (array_like or tuple of image info, optional) – Array, of shape (N,N), to be written into the alpha channel of the output image. If not already uint8, will be converted (and scaled) into uint8. If not supplied, will be made fully opaque. Optionally, you can also specify a tuple that includes scaling information, in the form of (array_to_plot, min_value_to_scale, max_value_to_scale).

Examples

>>> red_channel = np.log10(frb[("gas", "temperature")])
>>> blue_channel = np.log10(frb[("gas", "density")])
>>> multi_image_composite("multi_channel1.png", red_channel, blue_channel)
yt.visualization.image_writer.scale_image(image, mi=None, ma=None)[source]

Scale an image ([NxNxM] where M = 1-4) to be uint8 and values scaled from [0,255].

Parameters:

image (array_like or tuple of image info) –

Examples

>>> image = scale_image(image)
>>> image = scale_image(image, min=0, max=1000)
yt.visualization.image_writer.splat_points(image, points_x, points_y, contribution=None, transposed=False)[source]
yt.visualization.image_writer.write_bitmap(bitmap_array, filename, max_val=None, transpose=False)[source]

Write out a bitmapped image directly to a PNG file.

This accepts a three- or four-channel bitmap_array. If the image is not already uint8, it will be scaled and converted. If it is four channel, only the first three channels will be scaled, while the fourth channel is assumed to be in the range of [0,1]. If it is not four channel, a fourth alpha channel will be added and set to fully opaque. The resultant image will be directly written to filename as a PNG with no colormap applied. max_val is a value used if the array is passed in as anything other than uint8; it will be the value used for scaling and clipping in the first three channels when the array is converted. Additionally, the minimum is assumed to be zero; this makes it primarily suited for the results of volume rendered images, rather than misaligned projections.

Parameters:
  • bitmap_array (array_like) – Array of shape (N,M,3) or (N,M,4), to be written. If it is not already a uint8 array, it will be scaled and converted to uint8.

  • filename (string) – Filename to save to. If None, PNG contents will be returned as a string.

  • max_val (float, optional) – The upper limit to clip values to in the output, if converting to uint8. If bitmap_array is already uint8, this will be ignore.

  • transpose (boolean, optional) – If transpose is False, we assume that the incoming bitmap_array is such that the first element resides in the upper-left corner. If True, the first element will be placed in the lower-left corner.

yt.visualization.image_writer.write_image(image, filename, color_bounds=None, cmap_name=None, func=<function <lambda>>)[source]

Write out a floating point array directly to a PNG file, scaling it and applying a colormap.

This function will scale an image and directly call libpng to write out a colormapped version of that image. It is designed for rapid-fire saving of image buffers generated using yt.visualization.api.FixedResolutionBuffers and the likes.

Parameters:
  • image (array_like) – This is an (unscaled) array of floating point values, shape (N,N,) to save in a PNG file.

  • filename (string) – Filename to save as.

  • color_bounds (tuple of floats, optional) – The min and max to scale between. Outlying values will be clipped.

  • cmap_name (string, optional) – An acceptable colormap. See either yt.visualization.color_maps or https://scipy-cookbook.readthedocs.io/items/Matplotlib_Show_colormaps.html .

  • func (function, optional) – A function to transform the buffer before applying a colormap.

Returns:

scaled_image

Return type:

uint8 image that has been saved

Examples

>>> sl = ds.slice(0, 0.5, "Density")
>>> frb1 = FixedResolutionBuffer(sl, (0.2, 0.3, 0.4, 0.5), (1024, 1024))
>>> write_image(frb1[("gas", "density")], "saved.png")
yt.visualization.image_writer.write_projection(data, filename, colorbar=True, colorbar_label=None, title=None, vmin=None, vmax=None, take_log=True, figsize=(8, 6), dpi=100, cmap_name=None, extent=None, xlabel=None, ylabel=None)[source]

Write a projection or volume rendering to disk with a variety of pretty parameters such as limits, title, colorbar, etc. write_projection uses the standard matplotlib interface to create the figure. N.B. This code only works after you have created the projection using the standard framework (i.e. the Camera interface or off_axis_projection).

Accepts an NxM sized array representing the projection itself as well as the filename to which you will save this figure. Note that the final resolution of your image will be a product of dpi/100 * figsize.

Parameters:
  • data (array_like) – image array as output by off_axis_projection or camera.snapshot()

  • filename (string) – the filename where the data will be saved

  • colorbar (boolean) – do you want a colorbar generated to the right of the image?

  • colorbar_label (string) – the label associated with your colorbar

  • title (string) – the label at the top of the figure

  • vmin (float or None) – the lower limit of the zaxis (part of matplotlib api)

  • vmax (float or None) – the lower limit of the zaxis (part of matplotlib api)

  • take_log (boolean) – plot the log of the data array (and take the log of the limits if set)?

  • figsize (array_like) – width, height in inches of final image

  • dpi (int) – final image resolution in pixels / inch

  • cmap_name (string) – The name of the colormap.

Examples

>>> image = off_axis_projection(ds, c, L, W, N, "Density", no_ghost=False)
>>> write_projection(
...     image,
...     "test.png",
...     colorbar_label="Column Density (cm$^{-2}$)",
...     title="Offaxis Projection",
...     vmin=1e-5,
...     vmax=1e-3,
...     take_log=True,
... )