unmap package#

Submodules#

unmap.unmap module#

unmap.py

Functions for recoving data from images with a known colourmap.

unmap.unmap.check_arr(arr)[source]#

Check the input array, and return it as a NumPy array, and the alpha channel

Parameters:

arr – NumPy array of image data.

Returns:

The RGB image array, and the alpha channel.

unmap.unmap.crop_out(arr, rect)[source]#

Crop pixels out of a larger image.

unmap.unmap.get_background_rgb(arr, background='w')[source]#

Get the RGB value of the background colour.

Currently does not deal with alpha channel, if bg is transparent.

Could also accept a pixel location to get the colour from.

Parameters:
  • arr – NumPy array of image data.

  • background – The background colour: any matplotlib or CSS colour name, or ‘common’ to use the most common colour in the image.

Returns:

The RGB value of the background colour.

unmap.unmap.get_cmap(cmap, arr=None, levels=256, quantize=False)[source]#

Turn whatever we get as a colourmap into a matplotlib cmap.

Parameters:
  • cmap (str or array-like) – The colourmap to use. If a string, it must be a matplotlib colourmap name. If an array-like, it must be either a 4-tuple of pixel coordinates, or an Nx3 array of RGB values in the range (0-1). If you pass pixel coordinates, they must be ordered like (left, top, right, bottom), giving the coordinates of the colourmap in the image array arr (which you must also pass!).

  • arr (2d array) – The image array, if cmap is a 4-tuple of pixel coordinates.

  • levels (int) – The number of colours you want in the colourmap, or which you count in the colourmap if quantize is True.

  • quantize (bool) – Whether to quantize the colourmap to levels colours. Set this to true if the colourmap array or pixel lcoations you pass are ‘stepped’ or contain image noise.

Returns:

A matplotlib colourmap.

unmap.unmap.is_greyscale(arr, epsilon=1e-06)[source]#

Check if the image is greyscale.

Parameters:
  • arr – NumPy array of image data.

  • epsilon – The maximum difference between the R, G and B channels for the image to be considered greyscale.

Returns:

True if the image is greyscale, False otherwise.

unmap.unmap.normalize(arr, vrange)[source]#

Normalize the array to the given range.

Parameters:
  • arr – NumPy array of image data.

  • vrange – The range to normalize to, as a tuple (min, max).

Returns:

The normalized array.

unmap.unmap.remove_hillshade(img)[source]#

Pass in an RGB 3-channel image, scaled 0-1.

Returns the original image without the hillshading, and an RGBA array with only the hillshade.

This will only work if the colourmap only uses full-valued colours. In other words, the only thing affecting the lightness in the image is the hillshade. If the colourmap has any dark colours, this may be a problem.

Parameters:

img – NumPy array of image data.

Returns:

The image without the hillshade, and the hillshade as a separate

RGBA array.

unmap.unmap.unmap(arr, cmap=None, crop=None, vrange=(0, 1), levels=256, nan_colours=None, background='w', threshold=0.1, hillshade=False, quantize=False)[source]#

Unmap data, via a colourmap, from an image. Reverse false-colouring.

Parameters:
  • arr – NumPy array of image data.

  • cmap – Either another array, or pixel extent of colourbar in image, or name of mpl colourbar or if None, try to guess it.

  • crop – If not None, crop the image to the given rectangle. Given as a tuple (left, top, right, bottom).

  • vrange – (vmin, vmax) for the colourbar.

  • levels – Number of colours / value levels desired, default 256.

  • nan_colours – Colours to turn to NaN? Treat essentially like background. Combine these args?

  • background – Give background pixel location so can get this, or can be ‘white’/w or ‘black’/k, or RGB, or use the most common pixel colour with ‘common’.

  • threshold – 0 is exact match only, 1.732 is maximum distance and admits all points.

  • hillshade – Then use HSV

Returns:

A NumPy array of the same shape as the input image, with the colourmap removed. Essentially a greyscale image representing the data used to make the false colour image.

unmap.unweave module#

unweave.py

Functions for ‘unweaving the rainbow’: recovering colour maps from images with no a priori knowledge. Refactored from an earlier version.

unmap.unweave.construct_graph(imarray, colors=256, normed=True)[source]#

Construct an undirected value adjacency graph from an image array.

Weights are the number of times a pair of values co-occur in the image, normalized per value (i.e. per node in the graph).

Parameters:
  • imarray (np.ndarray) – Array of values.

  • colors (int) – Number of colours in the image.

  • normed (bool) – Whether to normalize the weights.

Returns:

Value adjacency graph.

Return type:

G (nx.Graph)

unmap.unweave.convert_imarray(imarray, colors=256)[source]#

Convert an RGB image array to an index array and colourtable. The array will be quantized to the specified number of colours, and will be no larger than 512x512 pixels.

Parameters:
  • imarray (np.ndarray) – The RGB or RGBA image array.

  • colors (int) – Number of colours to reduce to.

Returns:

Array of indices into the colourtable. unique_colors (np.ndarray): Colourtable.

Return type:

imarray (np.ndarray)

unmap.unweave.guess_cmap_from_array(array, source_colors=256, target_colors=256, min_weight=0.025, max_dist=0.25, max_neighbours=20, reverse='auto', equilibrate=False)[source]#

Guess the colormap of an image.

Parameters:
  • array (np.ndarray) – The RGB or RGBA image array.

  • source_colors (int) – Number of colours to detect in the source image.

  • target_colors (int) – Number of colours to return in the colormap.

  • min_weight (float) – Minimum weight to keep. See prune_graph.

  • max_dist (float) – Maximum distance to keep. See prune_graph.

  • max_neighbours (int) – Maximum number of neighbours to allow. See prune_graph.

  • reverse (bool) – Whether to reverse the colormap. If ‘auto’, the colormap will start with the end closest to dark blue. If False, the direction is essentially random.

unmap.unweave.guess_cmap_from_image(fname, source_colors=256, target_colors=256, min_weight=0.025, max_dist=0.25, max_neighbours=20, reverse='auto', equilibrate=False)[source]#

Guess the colormap of an image.

Parameters:
  • fname (str) – Filename or URL of image to guess.

  • source_colors (int) – Number of colours to detect in the source image.

  • target_colors (int) – Number of colours to return in the colormap.

  • min_weight (float) – Minimum weight to keep. See prune_graph.

  • max_dist (float) – Maximum distance to keep. See prune_graph.

  • max_neighbours (int) – Maximum number of neighbours to allow. See prune_graph.

  • reverse (bool) – Whether to reverse the colormap. If ‘auto’, the colormap will start with the end closest to dark blue. If False, the direction is essentially random.

unmap.unweave.longest_shortest_path(G)[source]#

Find the longest shortest path in a graph. This should be the path between the ends of the longest chain in the graph.

Parameters:

G (nx.Graph) – Graph to search.

Returns:

Longest shortest path.

Return type:

path (list)

unmap.unweave.ordered_unique(seq)[source]#
unmap.unweave.path_to_cmap(path, unique_colors, colors=256, reverse='auto', equilibrate=False)[source]#

Convert a path through the graph to a colormap.

Parameters:
  • path (list) – Path to convert.

  • unique_colors (np.ndarray) – Colourtable.

  • colors (int) – Number of colours to return. Default is 256. Use None to use twice the number of colours in the path.

  • reverse (bool) – Whether to reverse the colormap. If ‘auto’, the colormap will start with the end closest to dark blue. If False, the direction is essentially random.

  • equilibrate (bool) – Whether to equilibrate the colormap. This will try to ensure that the colormap’s colors are as evenly spaced as possible.

Returns:

Colormap.

Return type:

matplotlib.colors.LinearSegmentedColormap

unmap.unweave.plot_graph(G, unique_colors, layout='kamada_kawai', ax=None, figsize=(12, 8))[source]#

Plot a graph with colours.

Parameters:
  • G (nx.Graph) – Graph to plot.

  • unique_colors (np.ndarray) – Colourtable.

  • layout (str) – Layout to use.

  • ax (matplotlib.axes.Axes) – Axes to plot on.

  • figsize (tuple) – Figure size.

Returns:

Axes.

Return type:

ax (matplotlib.axes.Axes)

unmap.unweave.prune_graph(G, unique_colors, min_weight=0.025, max_dist=0.25, max_neighbours=20)[source]#

Prune a graph to remove edges with low weight and high distance.

Parameters:
  • G (nx.Graph) – Graph to prune.

  • unique_colors (np.ndarray) – Colourtable.

  • min_weight (float) – Minimum weight to keep.

  • max_dist (float) – Maximum distance to keep.

  • max_neighbours (int) – Maximum number of neighbours to allow. Nodes with more neighbours than this will be removed.

Returns:

Pruned graph.

Return type:

G (nx.Graph)

Module contents#