Image Stencil – How to Make Stencil in Python

What is Image Stencil?

An image stencil is a simplified visual representation of an image converted into outlines or shapes. This process, known as image stenciling, involves transforming a raster image into a stencil or template for creating designs or artwork. Typically, this is achieved through vectorization, which converts the raster image into a vector graphic composed of points, lines, and curves. This technique allows for the creation of pictures suitable for various applications in art, design, and industry.

Image stenciling is widely used in art and design to create precise, repeatable designs with high accuracy. It’s also common in printing, where stencils apply ink or other media to surfaces. Additionally, stenciling is practical in various fields, such as construction, where it is used to paint road lines or building markings. This technique enables efficient and accurate design creation from raster images.

How to Make Stencil

Aspose.SVG for Python via .NET provides an API for creating stenciling effects from images by vectorizing them into graphics made of points, lines, and curves. Creating an image stencil starts with uploading your image and applying vectorization algorithms to it - quantizing its color, contour tracing to extract shapes, etc. Additionally, you can adjust vectorization settings, such as applying trace smoothing, to eliminate jagged edges and produce a polished image.

Once the stencil is created, the vectorized image can be exported as an SVG file, serving as a template for the stencil effect. This template can then be used to create the desired design, either manually or using digital design tools. Aspose.SVG for Python via .NET offers a powerful, user-friendly interface for efficient and high-quality stencil creation from raster images, making it an invaluable tool for artists and designers.

To experience creating stencils from images, you can use the web-based application Stencil Drawing.

Create Image Stencil – Python Code

Here is a Python code example of how to turn a PNG image into a stencil using the Aspose.SVG Python library. You should follow a few steps:

  1. Initialize an instance of the BezierPathBuilder class to configure image vectorization and define the path-building strategy.
    • Set the trace_smoother property to smooth out fragments of contours.
    • Set the error_threshold property to define the allowable error in path approximation.
    • Specify the max_iterations property to control the number of iterations for path building.
  2. Create an instance of the ImageVectorizer class. The ImageVectorizer is the main class used to convert images to vector graphics.
    • Set the path_builder property to the previously configured BezierPathBuilder instance. This means the vectorizer will use the settings specified in the path_builder for path creation and optimization.
    • Specify the colors_limit property to limit the number of colors used in the vectorization.
    • Set the line_width property to define the width of the vector lines.
  3. Initialize an instance of the StencilConfiguration class to configure image stencil.
    • Set the type property to StencilType.MONO_COLOR. This will create a stencil with a single, uniform color for the outline.
    • Set the color property to a specific RGB color using Color.from_rgb() method.
    • Assign the configured StencilConfiguration instance to the stencil property of the vectorizer’s configuration.
  4. Use the vectorize() method of the ImageVectorizer class, providing the path to the image file. This method returns an SVGDocument.
  5. Call the save() method of the SVGDocument class to save the vectorized image as an SVG file, specifying the output path.
 1import os
 2from aspose.svg import *
 3from aspose.svg.drawing import *
 4from aspose.svg.rendering.image import *
 5from aspose.svg.imagevectorization import *
 6
 7# Setup directories
 8input_folder = "data/"
 9output_folder = "output/"
10if not os.path.exists(output_folder):
11    os.makedirs(output_folder)
12
13# Configuration for image vectorization
14path_builder = BezierPathBuilder()
15path_builder.trace_smoother = ImageTraceSmoother(2)
16path_builder.error_threshold = 20.0
17path_builder.max_iterations = 10
18
19vectorizer = ImageVectorizer()
20vectorizer.configuration.path_builder = path_builder
21vectorizer.configuration.colors_limit = 5
22vectorizer.configuration.line_width = 1.0
23
24# Configuration for image stencil
25stencil_config = StencilConfiguration()
26stencil_config.type = StencilType.MONO_COLOR
27stencil_config.color = Color.from_rgb(0, 0, 255)
28vectorizer.configuration.stencil = stencil_config
29
30# Vectorize an image
31src_file = "image.png"
32with vectorizer.vectorize(os.path.join(input_folder, src_file)) as document:
33    output_file = os.path.join(output_folder, "image-stencil.svg")
34    document.save(output_file)

Aspose.SVG offers a Free Online Image Vectorizer that is designed to convert bitmap images such as JPG, PNG, BMP, TIFF, and GIF into vector graphics. After conversion, all vector graphic elements are saved as SVG files. Our free vectorizer works on any platform. With this app, you can apply various options to achieve the perfect result. Save time and experience the advantages of vector graphics with our free Image Vectorizer!

Text “Image Vectorizer”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.