sketchkit.stylization.brushstrokesengine package

Subpackages

Submodules

sketchkit.stylization.brushstrokesengine.brushstroke_stylizer module

Brushstroke Stylizer for sketch-to-stylized image conversion.

class sketchkit.stylization.brushstrokesengine.brushstroke_stylizer.BrushstrokeStylizer(model_name: str = 'style2', style_id: str = 'playdoh10', models_dir: str | Path | None = None, device: str = 'cuda', auto_download: bool = True, **kwargs: Any)[source]

Bases: object

A class for stylizing sketches using the Neural Brushstroke Engine (NeuBE).

This class provides functionality to convert vector sketches into stylized images using various brushstroke styles from the NeuBE model.

model_name

The model name to use (‘style1’ or ‘style2’).

Type:

str

style_id

The style ID to use for stylization.

Type:

str

models_dir

Directory containing the models.

Type:

Path

device

Device to use (for future use, currently not used).

Type:

str

_get_option(name: str, kwargs: Dict[str, Any])[source]

Get option value from kwargs or fall back to instance default.

_import_forger()[source]

Import forger module using relative imports.

Since stylizer.py and forger/thirdparty are in the same directory, we can use relative imports for cleaner code.

The forger module internally uses absolute imports (import forger.xxx), so we need to register ‘forger’ and ‘thirdparty’ in sys.modules as top-level modules before importing.

Returns:

The forger module.

Raises:

ImportError – If forger module is not found.

_render_sketch_to_png(sketch: Sketch, output_dir: Path, canvas_size: int | None = None) str[source]

Render sketch to PNG using CairoRenderer.

Parameters:
  • sketch – Input vector sketch.

  • output_dir – Directory to save the rendered PNG.

  • canvas_size – Canvas size for rendering. If None, uses sketch’s actual width and height.

Returns:

Path to the rendered PNG file.

_run_forger_stylization(png_path: str, style_id: str, library_path: str | Path | None = None, output_dir: Path | None = None, output_file: str | Path | None = None, **kwargs: Any) str[source]

Run forger stylization by directly calling forger functions.

This method directly imports and uses forger modules without requiring pip install -e . Only needs the brushstroke_engine directory in the filesystem.

run(sketch: Sketch, output_path: str | None = None, canvas_size: int | None = None, style_id: str | None = None, library_path: str | Path | None = None, **kwargs: Any) Image[source]

Stylize a sketch using the brushstroke engine.

Parameters:
  • sketch – Input vector sketch.

  • output_path – Output file path. If None, uses a temporary path.

  • canvas_size – Canvas size for rendering. If None, uses sketch’s actual width and height.

  • style_id – Style ID to use. If None, uses the instance default.

  • library_path – Path to brush library. If None, uses default.

  • **kwargs – Additional parameters for stylization: - feature_blending_level (int): Feature blending level. Defaults to 2. - color_mode (str): Color mode. Defaults to ‘1’. - crop_margin (int): Crop margin. Defaults to 10. - on_white (bool): Render on white background. Defaults to True.

Returns:

Stylized image as PIL Image.

run_from_png(png_path: str | Path, output_path: str | None = None, style_id: str | None = None, library_path: str | Path | None = None, **kwargs: Any) Image[source]

Stylize an existing PNG geometry image using the brushstroke engine.

This method assumes the input PNG is a line drawing (black on white), similar to the –geom_image expected by sketchkit.stylizer.brushstrokesengine.forger.viz.paint_image_main.

Parameters:
  • png_path – Path to the input PNG geometry image.

  • output_path – Output file path. If None, uses a temporary path.

  • style_id – Style ID to use. If None, uses the instance default.

  • library_path – Path to brush library. If None, uses default.

  • **kwargs – Additional parameters for stylization (same as run()).

Returns:

Stylized image as PIL Image.

sketchkit.stylization.brushstrokesengine.models module

Model download utilities for Brushstroke Engine.

sketchkit.stylization.brushstrokesengine.models.check_models_available(model_name: str = 'style2', models_dir: Path | None = None) bool[source]

Check if models are available for the specified model name.

Parameters:
  • model_name – Name of the model (‘style1’ or ‘style2’).

  • models_dir – Directory containing models. If None, uses default location.

Returns:

True if models are available, False otherwise.

sketchkit.stylization.brushstrokesengine.models.download_models(models_dir: Path | None = None, use_s3: bool = True, quiet: bool = False) Path[source]

Download and extract brushstroke engine models.

Parameters:
  • models_dir – Directory to download models to. If None, uses default location.

  • use_s3 – If True, use S3 URL; otherwise use Google Drive.

  • quiet – If True, suppress download progress.

Returns:

Path to the models directory.

sketchkit.stylization.brushstrokesengine.models.get_model_path(model_name: str = 'style2', models_dir: Path | None = None) Path[source]

Get the path to a model checkpoint.

Parameters:
  • model_name – Name of the model (‘style1’ or ‘style2’).

  • models_dir – Directory containing models. If None, uses default location.

Returns:

Path to the model checkpoint directory.

Raises:

FileNotFoundError – If the model is not found.

sketchkit.stylization.brushstrokesengine.models.get_models_dir() Path[source]

Get the default models directory path.

Returns:

Path to the models directory (sketchkit/stylizer/brushstrokesengine/models/)

Module contents

Brushstrokes Engine implementation for stylization.

This module provides the brushstrokes engine method for stylizing sketches.

class sketchkit.stylization.brushstrokesengine.BrushstrokeStylizer(model_name: str = 'style2', style_id: str = 'playdoh10', models_dir: str | Path | None = None, device: str = 'cuda', auto_download: bool = True, **kwargs: Any)[source]

Bases: object

A class for stylizing sketches using the Neural Brushstroke Engine (NeuBE).

This class provides functionality to convert vector sketches into stylized images using various brushstroke styles from the NeuBE model.

model_name

The model name to use (‘style1’ or ‘style2’).

Type:

str

style_id

The style ID to use for stylization.

Type:

str

models_dir

Directory containing the models.

Type:

Path

device

Device to use (for future use, currently not used).

Type:

str

_get_option(name: str, kwargs: Dict[str, Any])[source]

Get option value from kwargs or fall back to instance default.

_import_forger()[source]

Import forger module using relative imports.

Since stylizer.py and forger/thirdparty are in the same directory, we can use relative imports for cleaner code.

The forger module internally uses absolute imports (import forger.xxx), so we need to register ‘forger’ and ‘thirdparty’ in sys.modules as top-level modules before importing.

Returns:

The forger module.

Raises:

ImportError – If forger module is not found.

_render_sketch_to_png(sketch: Sketch, output_dir: Path, canvas_size: int | None = None) str[source]

Render sketch to PNG using CairoRenderer.

Parameters:
  • sketch – Input vector sketch.

  • output_dir – Directory to save the rendered PNG.

  • canvas_size – Canvas size for rendering. If None, uses sketch’s actual width and height.

Returns:

Path to the rendered PNG file.

_run_forger_stylization(png_path: str, style_id: str, library_path: str | Path | None = None, output_dir: Path | None = None, output_file: str | Path | None = None, **kwargs: Any) str[source]

Run forger stylization by directly calling forger functions.

This method directly imports and uses forger modules without requiring pip install -e . Only needs the brushstroke_engine directory in the filesystem.

run(sketch: Sketch, output_path: str | None = None, canvas_size: int | None = None, style_id: str | None = None, library_path: str | Path | None = None, **kwargs: Any) Image[source]

Stylize a sketch using the brushstroke engine.

Parameters:
  • sketch – Input vector sketch.

  • output_path – Output file path. If None, uses a temporary path.

  • canvas_size – Canvas size for rendering. If None, uses sketch’s actual width and height.

  • style_id – Style ID to use. If None, uses the instance default.

  • library_path – Path to brush library. If None, uses default.

  • **kwargs – Additional parameters for stylization: - feature_blending_level (int): Feature blending level. Defaults to 2. - color_mode (str): Color mode. Defaults to ‘1’. - crop_margin (int): Crop margin. Defaults to 10. - on_white (bool): Render on white background. Defaults to True.

Returns:

Stylized image as PIL Image.

run_from_png(png_path: str | Path, output_path: str | None = None, style_id: str | None = None, library_path: str | Path | None = None, **kwargs: Any) Image[source]

Stylize an existing PNG geometry image using the brushstroke engine.

This method assumes the input PNG is a line drawing (black on white), similar to the –geom_image expected by sketchkit.stylizer.brushstrokesengine.forger.viz.paint_image_main.

Parameters:
  • png_path – Path to the input PNG geometry image.

  • output_path – Output file path. If None, uses a temporary path.

  • style_id – Style ID to use. If None, uses the instance default.

  • library_path – Path to brush library. If None, uses default.

  • **kwargs – Additional parameters for stylization (same as run()).

Returns:

Stylized image as PIL Image.