Images (image)

Functions for handling and generating images.

class vzlog.image.ImageGrid(data=None, rows=None, cols=None, shape=None, border_color=1, border_width=1, cmap=None, vmin=None, vmax=None, vsym=False, global_bounds=True)

An image grid used for combining equally-sized intensity images into a single larger image.

Parameters:
  • data (ndarray, ndim in [2, 3, 4]) – The last two axes should be spatial dimensions of an intensity patch. The rest are used to index them. If ndim is 2, then a single image is shown. If ndim is 3, then rows and cols will determine its layout. If ndim is 4, then rows and cols will be ignored and the grid will be layed out according to its first two axes instead. If data is set to None, then an empty image grid will be initialized. In this case, rows and cols are both required.
  • rows/cols (int or None) – The number of rows and columns for the grid. If both are None, the minimal square grid that holds all images will be used. If one is specified, the other will adapt to hold all images. If both are specified, then it possible that the grid will be vacuous or that some images will be omitted.
  • shape (tuple) – Shape of the each grid image. Only use if data is set to None, since it should otherwise be inferred from the data.
  • border_color (float or np.ndarray of length 3) – Specify the border color as an array of length 3 (RGB). If a scalar is given, it will be interpreted as the grayscale value.
  • border_width – Border with in pixels. If you rescale the image, the border will be rescaled with it.
  • cmap/vmin/vmax/vsym – See ImageGrid.set_image.
  • global_bounds (bool) – If this is set to True and either vmin or vmax is not specified, it will infer it globally for the data. If vsym is True, the global bounds will be symmetric around zero. If it is set to False, it determines range per image, which would be the equivalent of calling set_image manually with vmin, vmax and vsym set the same.

Examples

>>> import vzlog
>>> import numpy as np
>>> import matplotlib.pylab as plt
>>> from matplotlib.pylab import cm
>>> rs = np.random.RandomState(0)

Let’s generate a set of 100 8x8 image patches.

>>> shape = (100, 8, 8)
>>> data = np.arange(np.prod(shape)).reshape(shape)
>>> data += rs.uniform(0, np.prod(shape), size=shape)

Creating the image grid:

>>> grid = vzlog.image.ImageGrid(data, cmap=cm.hsv)
>>> img = grid.scaled_image(scale=5)
>>> plt.imshow(img)
>>> plt.show()

If you are working in an IPython notebook, you can display img simply by adding it to the end of a cell.

image

Returns the image as a skimage.io.Image class.

save(path, scale=1)

Save the image to file.

Parameters:
  • path (str) – Output path.
  • scale (int) – Upscaling using nearest neighbor, e.g. a scale of 5 will make each pixel a 5x5 rectangle in the output.
scaled_image(scale=1)

Returns a nearest-neighbor upscaled scaled version of the image.

Parameters:scale (int) – Upscaling using nearest neighbor, e.g. a scale of 5 will make each pixel a 5x5 rectangle in the output.
Returns:scaled_image – Returns a scaled up RGB image. If you do not have scikit-image, it will be returned as a regular Numpy array. The benefit of wrapping it in Image, is so that it will be automatically displayed in IPython notebook, without having to issue any drawing calls.
Return type:skimage.io.Image, (height, width, 3)
set_image(image, row, col, cmap=None, vmin=None, vmax=None, vsym=False)

Sets the data for a single window.

Parameters:
  • image (ndarray, ndim=2) – The shape should be the same as the shape specified when constructing the image grid.
  • row/col (int) – The zero-index of the row and column to set.
  • cmap (cmap (from matplotlib.pylab.cm)) – The color palette to use. Default is grayscale.
  • vmin/vmax (numerical or None) – Defines the range of the color palette. None, which is default, takes the range of the data.
  • vsym (bool) – If True, this means that the color palette will always be centered around 0. Even if you have specified both vmin and vmax, this will override that and extend the shorter one. Good practice is to specify neither vmin or vmax or only vmax together with this option.
class vzlog.image.ColorImageGrid(data=None, rows=None, cols=None, shape=None, border_color=1, border_width=1, vmin=0.0, vmax=1.0, vsym=False, global_bounds=True)

An image grid used for combining equally-sized RGB images into a single larger image. It is similar to ImageGrid, except it only works for RGB images with color channels scaled in [0, 1].

Parameters:
  • data (ndarray, ndim in [3, 4, 5]) – The last three axes should be spatial dimensions and a color channel. The rest are used to index them. If ndim is 3, then a single image is shown. If ndim is 4, then rows and cols will determine its layout. If ndim is 5, then rows and cols will be ignored and the grid will be layed out according to its first two axes instead. If data is set to None, then an empty image grid will be initialized. In this case, rows and cols are both required.
  • rows/cols (int or None) – The number of rows and columns for the grid. If both are None, the minimal square grid that holds all images will be used. If one is specified, the other will adapt to hold all images. If both are specified, then it possible that the grid will be vacuous or that some images will be omitted.
  • shape (tuple) – Shape of the each grid image. Only use if data is set to None, since it should otherwise be inferred from the data.
  • border_color (float or np.ndarray of length 3) – Specify the border color as an array of length 3 (RGB). If a scalar is given, it will be interpreted as the grayscale value.
  • border_width – Border with in pixels. If you rescale the image, the border will be rescaled with it.
  • cmap/vmin/vmax/vsym – See ImageGrid.set_image.
  • global_bounds (bool) – If this is set to True and either vmin or vmax is not specified, it will infer it globally for the data. If vsym is True, the global bounds will be symmetric around zero. If it is set to False, it determines range per image, which would be the equivalent of calling set_image manually with vmin, vmax and vsym set the same.
image

Returns the image as a skimage.io.Image class.

save(path, scale=1)

Save the image to file.

Parameters:
  • path (str) – Output path.
  • scale (int) – Upscaling using nearest neighbor, e.g. a scale of 5 will make each pixel a 5x5 rectangle in the output.
scaled_image(scale=1)

Returns a nearest-neighbor upscaled scaled version of the image.

Parameters:scale (int) – Upscaling using nearest neighbor, e.g. a scale of 5 will make each pixel a 5x5 rectangle in the output.
Returns:scaled_image – Returns a scaled up RGB image. If you do not have scikit-image, it will be returned as a regular Numpy array. The benefit of wrapping it in Image, is so that it will be automatically displayed in IPython notebook, without having to issue any drawing calls.
Return type:skimage.io.Image, (height, width, 3)
set_image(image, row, col, vmin=0.0, vmax=1.0, vsym=False)

Sets the data for a single window.

Parameters:
  • image (ndarray, ndim=3) – The shape should be the same as the shape specified when constructing the image grid, plus an axis of length 3 for the color channels.
  • row/col (int) – The zero-index of the row and column to set.
  • vmin/vmax (numerical or None) – Defines the range of the color palette. None, takes the range of the data. Default is vmin=0 and vmax=1, for plotting normal RGB images.
  • vsym (bool) – If True, this means that the color palette will always be centered around 0. Even if you have specified both vmin and vmax, this will override that and extend the shorter one. Good practice is to specify neither vmin or vmax or only vmax together with this option.