Create Image Stencils in Python

To create an image stencil in Python, configure ImageVectorizer with BezierPathBuilder and StencilConfiguration, set the stencil type and color, call vectorize(input_path), and save the returned SVGDocument as an SVG file.

An image stencil is a simplified vector version of a raster image. Instead of preserving every color and detail, stencil output focuses on clean shapes, outlines, or limited-color regions that can be used for design, print, cutting, templates, and stylized artwork.

When to Use Image Stenciling

Use image stenciling when the final SVG should be intentionally simplified. It is a good fit for artwork that needs strong contours, limited colors, or a single-color graphic style. Logos, icons, signs, line drawings, and high-contrast illustrations usually produce cleaner stencils than noisy photos.

If you want to preserve more source colors and details, use normal image vectorization instead.

Aspose.SVG APIs Used

APIPurpose
ImageVectorizerConverts the raster source to an SVG document
ImageVectorizerConfigurationStores vectorization and stencil settings
BezierPathBuilderBuilds vector paths from traced contours
ImageTraceSmootherSmooths traced contours before path construction
StencilConfigurationConfigures stencil output
StencilTypeDefines the stencil mode
ColorSets the stencil color

Create Image Stencil in Python

The following example turns image.png into a mono-color stencil SVG. It configures smoothing and path approximation, limits the color count used during vectorization, and applies StencilConfiguration to produce stencil-style output.

 1import os
 2from aspose.svg.drawing import Color
 3from aspose.svg.imagevectorization import (
 4    BezierPathBuilder,
 5    ImageTraceSmoother,
 6    ImageVectorizer,
 7    StencilConfiguration,
 8    StencilType,
 9)
10
11# Create a mono-color SVG stencil from a raster image
12input_folder = "data/"
13output_folder = "output/"
14input_path = os.path.join(input_folder, "image.png")
15output_path = os.path.join(output_folder, "image-stencil.svg")
16os.makedirs(output_folder, exist_ok=True)
17
18path_builder = BezierPathBuilder()
19path_builder.trace_smoother = ImageTraceSmoother(2)
20path_builder.error_threshold = 20.0
21path_builder.max_iterations = 10
22
23vectorizer = ImageVectorizer()
24vectorizer.configuration.path_builder = path_builder
25vectorizer.configuration.colors_limit = 5
26vectorizer.configuration.line_width = 1.0
27
28stencil_config = StencilConfiguration()
29stencil_config.type = StencilType.MONO_COLOR
30stencil_config.color = Color.from_rgb(0, 0, 255)
31vectorizer.configuration.stencil = stencil_config
32
33with vectorizer.vectorize(input_path) as document:
34    document.save(output_path)

The generated image-stencil.svg contains vector geometry styled according to the stencil configuration. Change Color.from_rgb() to set another stencil color, and adjust colors_limit, trace_smoother, and error_threshold to balance detail and simplicity.

Stencil Settings That Affect Output

SettingEffectPractical guidance
stencil_config.typeSelects the stencil modeUse StencilType.MONO_COLOR for single-color stencil artwork
stencil_config.colorSets the output stencil colorPick a high-contrast color for previews, print, or cutting workflows
colors_limitControls color quantization before stencil outputLower values simplify the source and usually produce cleaner stencil shapes
trace_smootherSmooths contour fragmentsIncrease it to reduce jagged edges, but check small details
error_thresholdControls path approximation toleranceLower values preserve more detail; higher values simplify paths
line_widthSets the generated line widthAdjust it to match the target visual scale

Common Mistakes and Fixes

ProblemLikely causeFix
Stencil has too much detailSource image is noisy or colors_limit is too highPreprocess the source, reduce colors, or increase smoothing
Important shapes disappearSimplification is too aggressiveLower smoothing or reduce error_threshold
Output looks jaggedContours were traced with too little smoothingIncrease trace_smoother and inspect the SVG at the target size
Photo output is complexContinuous-tone photos are difficult to simplify cleanlyUse high-contrast artwork or reduce the photo before vectorization

Related Articles