zennit.image

Functionality to convert arrays to images

Functions

get_cmap

Convenience function to lookup built-in color maps, or create color maps from a source code.

gridify

Align multiple arrays, described as an additional 0-th dimension, into a grid with the 0-th dimension removed.

imgify

Convert an array with 1 or 3 channels to a PIL image.

imsave

Convert an array to an image and save it using file fp.

palette

Convenience function to create palettes from built-in colormaps, or from a source code if necessary.

zennit.image.get_cmap(cmap)[source]

Convenience function to lookup built-in color maps, or create color maps from a source code.

Parameters

cmap (str or ColorMap) – String to specify a built-in color map, code used to create a new color map, or a ColorMap instance.

Returns

The built-in color map with key cmap in CMAPS, a new color map created from the code cmap, or cmap if it already was a ColorMap.

Return type

ColorMap

zennit.image.gridify(obj, shape=None, fill_value=None)[source]

Align multiple arrays, described as an additional 0-th dimension, into a grid with the 0-th dimension removed.

Parameters
  • obj (object) – An object that can be converted to a numpy array, with 3 (grayscale) or 4 (rgb) axes. The color channel’s position is automatically detected, and moved to the back of the shape.

  • shape (tuple of size 2, optional) – Height and width of the produced grid. If None (default), create a square grid.

  • fill_value (float or obj:numpy.ndarray) – A value to fill empty grid members. May be any compatible shape to obj.

Returns

obj – An array with the 0-th dimension absorbed into the height and width dimensions (then 0 and 1). The color dimension will be the last dimension, even if it was the first dimension before.

Return type

numpy.ndarray

zennit.image.imgify(obj, vmin=None, vmax=None, cmap='bwr', level=1.0)[source]

Convert an array with 1 or 3 channels to a PIL image. The color dimension can be either the first or the last dimension.

Parameters
  • obj (object) – Anything that can be converted to a numpy array with 2 dimensions grayscale, or 3 dimensions with 1 or 3 values in the first or last dimension (color).

  • vmin (float or obj:numpy.ndarray) – Minimum value of the array.

  • vmax (float or obj:numpy.ndarray) – Maximum value of the array.

  • cmap (str or ColorMap) – String to specify a built-in color map, code used to create a new color map, or a ColorMap instance, which will be used to create a palette. The color map will only be applied for arrays with only a single color channel. The color will be specified as a palette in the PIL Image.

  • level (float) – The level of the color map. 1.0 is default. Values below zero reduce the color range, with only a single color used at value 0.0. Values above 1.0 clip the value earlier towards the maximum, with an increasingly steep transition at the center of the image.

Returns

image – The array visualized as a Pillow Image.

Return type

obj:PIL.Image

zennit.image.imsave(fp, obj, vmin=None, vmax=None, cmap='bwr', level=1.0, grid=False, format=None, writer_params=None)[source]

Convert an array to an image and save it using file fp. Internally, imgify is called to create a PIL Image, which is then saved using PIL.

Parameters
  • fp (str, obj:pathlib.Path or file) – Save target for PIL Image.

  • obj (object) – Anything that can be converted to a numpy array with 2 dimensions grayscale, or 3 dimensions with 1 or 3 values in the first or last dimension (color).

  • vmin (float or obj:numpy.ndarray) – Minimum value of the array.

  • vmax (float or obj:numpy.ndarray) – Maximum value of the array.

  • cmap (str or ColorMap) – String to specify a built-in color map, code used to create a new color map, or a ColorMap instance, which will be used to create a palette. The color map will only be applied for arrays with only a single color channel. The color will be specified as a palette in the PIL Image.

  • level (float) – The level of the color map. 1.0 is default. Values below zero reduce the color range, with only a single color used at value 0.0. Values above 1.0 clip the value earlier towards the maximum, with an increasingly steep transition at the center of the image.

  • grid (bool) – If True, align multiple arrays (in dimension 0) into a grid before creating an image.

  • format (str) – Optional format override for PIL Image.save.

  • writer_params (dict) – Extra params to the image writer in PIL.

zennit.image.palette(cmap='bwr', level=1.0)[source]

Convenience function to create palettes from built-in colormaps, or from a source code if necessary.

Parameters
  • cmap (str or ColorMap) – String to specify a built-in color map, code used to create a new color map, or a ColorMap instance, which will be used to create a palette.

  • level (float) – The level of the color map palette. 1.0 is default. Values below zero reduce the color range, with only a single color used at value 0.0. Values above 1.0 clip the value earlier towards the maximum, with an increasingly steep transition at the center of the color map range.

Returns

obj – The palette described by an unsigned 8-bit numpy array with 256 entries.

Return type

numpy.ndarray