tag_word_cloud

Utilities for creating a word cloud.

Functions

create_wordcloud(word_dict[, mask_path, ...])

Takes a word dict and returns a generated word cloud object.

load_and_resize_mask(mask_path[, width, height])

Load a mask image and resize it according to given dimensions.

word_cloud_to_svg(wc)

Return a WordCould as an SVG string.

create_wordcloud(word_dict, mask_path=None, background_color=None, width=400, height=300, **kwargs)[source]

Takes a word dict and returns a generated word cloud object.

Parameters:
  • word_dict (dict) – words and their frequencies

  • mask_path (str or None) – The path of the mask file

  • background_color (str or None) – If None, transparent background.

  • width (int) – width in pixels.

  • height (int) – height in pixels.

  • kwargs (kwargs) – Any other parameters WordCloud accepts, overrides default values where relevant.

Returns:

The generated cloud. (Use .to_file to save it out as an image.)

Return type:

WordCloud

Raises:

ValueError – An empty dictionary was passed

load_and_resize_mask(mask_path, width=None, height=None)[source]

Load a mask image and resize it according to given dimensions.

The image is resized maintaining aspect ratio if only width or height is provided.

Returns None if no mask_path.

Parameters:
  • mask_path (str) – The path to the mask image file.

  • width (int, optional) – The desired width of the resized image. If only width is provided, the image is scaled to maintain its original aspect ratio. Defaults to None.

  • height (int, optional) – The desired height of the resized image. If only height is provided, the image is scaled to maintain its original aspect ratio. Defaults to None.

Returns:

The loaded and processed mask image as a numpy array with binary values (0 or 255).

Return type:

numpy.ndarray

word_cloud_to_svg(wc)[source]

Return a WordCould as an SVG string.

Parameters:

wc (WordCloud) – the word cloud object.

Returns:

The svg for the word cloud.

Return type:

svg_string (str)