Create Image Stencils in Python

Quick Answer: To create an image stencil or black-and-white vectorization result in Python, configure ImageVectorizer with BezierPathBuilder and StencilConfiguration, set the stencil type and output 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.

Create Image Stencil in Python

The following example turns tulip.png into a stencil-style SVG. It configures smoothing and path approximation, limits the color count used during vectorization, and applies StencilConfiguration with StencilType.AUTO so the vectorizer can choose the stencil behavior and colors automatically.

 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 an SVG stencil from a raster image using automatic stencil mode
12input_folder = "data/"
13output_folder = "output/"
14input_path = os.path.join(input_folder, "tulip.png")
15output_path = os.path.join(output_folder, "tulip-stencil.svg")
16os.makedirs(output_folder, exist_ok=True)
17
18path_builder = BezierPathBuilder()
19path_builder.trace_smoother = ImageTraceSmoother(3)
20path_builder.error_threshold = 20.0
21path_builder.max_iterations = 15
22
23vectorizer = ImageVectorizer()
24vectorizer.configuration.path_builder = path_builder
25vectorizer.configuration.colors_limit = 15
26vectorizer.configuration.line_width = 1.0
27
28stencil_config = StencilConfiguration()
29stencil_config.type = StencilType.AUTO
30vectorizer.configuration.stencil = stencil_config
31
32with vectorizer.vectorize(input_path) as document:
33    document.save(output_path)

The generated tulip-stencil.svg contains simplified vector geometry with colors selected automatically by StencilType.AUTO. This makes the example useful for showing a general stencil effect before the stricter black-and-white workflow below.

The illustration compares the raster tulip image (a) with the automatically generated color stencil result (b).

Raster tulip image and color SVG stencil created with StencilType.AUTO in Python

Create a Black-and-White SVG Stencil in Python

Black-and-white vectorization is a stencil workflow where the output intentionally uses one foreground color on a plain background instead of preserving the source palette. Use it for cutting templates, printable outlines, icons, signs, craft artwork, and high-contrast graphics.

The example below turns logo.png into a black stencil SVG. It uses StencilConfiguration with StencilType.MONO_COLOR, sets the stencil color to black, and keeps the color limit low so the source is simplified before the stencil effect is applied.

 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 black-and-white SVG stencil from a raster logo
12input_folder = "data/"
13output_folder = "output/"
14input_path = os.path.join(input_folder, "logo.png")
15output_path = os.path.join(output_folder, "logo-black-white-stencil.svg")
16os.makedirs(output_folder, exist_ok=True)
17
18path_builder = BezierPathBuilder()
19path_builder.trace_smoother = ImageTraceSmoother(2)
20path_builder.error_threshold = 12.0
21path_builder.max_iterations = 15
22
23vectorizer = ImageVectorizer()
24vectorizer.configuration.path_builder = path_builder
25vectorizer.configuration.colors_limit = 2
26vectorizer.configuration.line_width = 1.5
27
28stencil_config = StencilConfiguration()
29stencil_config.type = StencilType.MONO_COLOR
30stencil_config.color = Color.from_rgb(0, 0, 0)
31vectorizer.configuration.stencil = stencil_config
32
33with vectorizer.vectorize(input_path) as document:
34    document.save(output_path)

If the black-and-white stencil loses important shapes, reduce smoothing or lower error_threshold. If the output contains too many small fragments, simplify the input image first or increase smoothing slightly.

The illustration below shows how a flat raster logo is simplified into a high-contrast black-and-white SVG stencil.

Black-and-white SVG stencil created from a raster logo in Python

Note: StencilType.MONO_COLOR is not limited to black. Choose the foreground color in the line stencil_config.color = Color.from_rgb(0, 0, 0), for example by using RGB values for blue, red, green, or a brand color.

Stencil vs Multicolor Vectorization

Stencil output and normal image vectorization solve different problems. Normal multicolor vectorization tries to preserve visible color regions from the source image. A stencil intentionally discards most color information and produces simplified shapes that are easier to print, cut, trace, or reuse as monochrome artwork.

WorkflowOutput styleBest for
Normal image vectorizationMultiple color regions and pathsIllustrations, logos, diagrams, and SVG approximations of raster artwork
Black-and-white stencilOne foreground color with simplified shapesCutting templates, signs, icons, print masks, craft designs, and high-contrast artwork
Limited-color stencilA simplified stencil look with a chosen output colorBranded stencil artwork, previews, and stylized design assets

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

Stencil Settings That Affect Output

SettingEffectPractical guidance
stencil_config.typeSelects the stencil modeUse StencilType.AUTO for a general stencil result and StencilType.MONO_COLOR for one-color output
stencil_config.colorSets the output color for mono-color stencil modePick a high-contrast color when using StencilType.MONO_COLOR
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