CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-wordcloud

A little word cloud generator for creating visually appealing word clouds from text data.

Overview
Eval results
Files

color-generation.mddocs/

Color Generation and Styling

Advanced color customization capabilities for word clouds, including random color generation, single-color variations, matplotlib colormap integration, and image-based color extraction for sophisticated visual styling.

Capabilities

Random Color Generation

Default color generation function providing random hue variations with consistent saturation and lightness.

def random_color_func(
    word=None,
    font_size=None,
    position=None,
    orientation=None,
    font_path=None,
    random_state=None
):
    """
    Generate random HSL color for word cloud.

    This is the default color function that creates random hues with 80% saturation
    and 50% lightness for visually appealing color variations.

    Parameters:
    - word (str, optional): The word being colored (ignored)
    - font_size (int, optional): Font size of the word (ignored)
    - position (tuple, optional): Position of the word (ignored)
    - orientation (int, optional): Orientation of the word (ignored)
    - font_path (str, optional): Font path (ignored)
    - random_state (random.Random, optional): Random state for reproducible colors

    Returns:
    - str: HSL color string in format "hsl(hue, 80%, 50%)"
    """

Single Color Variations

Factory function for creating color functions that generate variations of a single base color.

def get_single_color_func(color):
    """
    Create color function that generates variations of a single base color.

    Returns a function that generates different brightness values while maintaining
    the same hue and saturation as the base color.

    Parameters:
    - color (str): Base color as PIL-compatible color string (e.g., 'deepskyblue', '#00b4d2')

    Returns:
    - callable: Color function that returns RGB color variations
    """

Matplotlib Colormap Integration

Class for creating color functions based on matplotlib colormaps for professional color schemes.

class colormap_color_func:
    def __init__(self, colormap):
        """
        Initialize colormap-based color function.

        Parameters:
        - colormap (str or matplotlib.colors.Colormap): Colormap name or object
        """

    def __call__(self, word, font_size, position, orientation, random_state=None, **kwargs):
        """
        Generate color from matplotlib colormap.

        Parameters:
        - word (str): The word being colored (ignored)
        - font_size (int): Font size of the word (ignored)
        - position (tuple): Position of the word (ignored)
        - orientation (int): Orientation of the word (ignored)
        - random_state (random.Random, optional): Random state for color selection
        - **kwargs: Additional arguments (ignored)

        Returns:
        - str: RGB color string in format "rgb(r, g, b)"
        """

Image-Based Color Generation

Advanced color generation that extracts colors from reference images for thematically consistent word clouds.

class ImageColorGenerator:
    def __init__(self, image, default_color=None):
        """
        Initialize image-based color generator.

        Creates a color function that samples colors from a reference image based on
        word positions, enabling color schemes that match specific images or themes.

        Parameters:
        - image (numpy.ndarray): RGB image array with shape (height, width, 3) or (height, width, 4)
        - default_color (tuple, optional): Fallback RGB color as (r, g, b) for out-of-bounds areas

        Raises:
        - ValueError: If image dimensions are invalid or canvas is larger than image without default_color
        """

    def __call__(self, word, font_size, font_path, position, orientation, **kwargs):
        """
        Generate color based on image region under word.

        Samples the average color from the image region that will be covered by the word,
        creating visually cohesive word clouds that match the reference image's color palette.

        Parameters:
        - word (str): The word being colored
        - font_size (int): Font size in pixels
        - font_path (str): Path to font file
        - position (tuple): Word position as (x, y)
        - orientation (int): Word orientation in degrees
        - **kwargs: Additional arguments (ignored)

        Returns:
        - str: RGB color string in format "rgb(r, g, b)"

        Raises:
        - ValueError: If word extends beyond image bounds and no default_color provided
        - NotImplementedError: For grayscale images (not yet supported)
        """

Usage Examples

Random Colors

from wordcloud import WordCloud, random_color_func

# Use default random colors
wc = WordCloud(color_func=random_color_func)
wc.generate(text)

Single Color Variations

from wordcloud import WordCloud, get_single_color_func

# Create variations of blue
blue_func = get_single_color_func('deepskyblue')
wc = WordCloud(color_func=blue_func)
wc.generate(text)

# Using hex color
red_func = get_single_color_func('#FF4500')
wc = WordCloud(color_func=red_func)
wc.generate(text)

Matplotlib Colormaps

from wordcloud import WordCloud

# Use built-in colormap support
wc = WordCloud(colormap='plasma')
wc.generate(text)

# Or create custom colormap function
from wordcloud import colormap_color_func
color_func = colormap_color_func('viridis')
wc = WordCloud(color_func=color_func)
wc.generate(text)

Image-Based Colors

from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
from PIL import Image

# Load reference image
image = np.array(Image.open('reference.jpg'))

# Create color generator
color_generator = ImageColorGenerator(image)

# Generate word cloud with image colors
wc = WordCloud(color_func=color_generator, mask=image)
wc.generate(text)
wc.to_file('colored_wordcloud.png')

Custom Color Functions

from wordcloud import WordCloud

# Define custom color function
def custom_color_func(word, font_size, position, orientation, **kwargs):
    # Color words based on length
    if len(word) > 6:
        return 'rgb(255, 100, 100)'  # Red for long words
    elif len(word) > 3:
        return 'rgb(100, 255, 100)'  # Green for medium words
    else:
        return 'rgb(100, 100, 255)'  # Blue for short words

wc = WordCloud(color_func=custom_color_func)
wc.generate(text)

Recoloring Existing Word Clouds

from wordcloud import WordCloud, get_single_color_func

# Generate word cloud
wc = WordCloud().generate(text)

# Recolor with different scheme
purple_func = get_single_color_func('purple')
wc.recolor(color_func=purple_func)

# Or use colormap
wc.recolor(colormap='cool')

Install with Tessl CLI

npx tessl i tessl/pypi-wordcloud

docs

cli-interface.md

color-generation.md

core-generation.md

index.md

text-processing.md

tile.json