Image Vectorization – Image to SVG
Vectorization involves converting bitmaps into mathematical curves and geometric shapes, which are then saved as SVG files. Aspose.SVG for Python via. NET API offers a high-speed library for various SVG parsing tasks, including image vectorization. Aspose.SVG offers versatile options for converting images to vector graphics both programmatically with the Aspose.SVG Python library and online with the real-time, high-quality Image Vectorizer.
Online Image Vectorizer
Ready to transform your pixel art into crisp vector graphics? Image Vectorizer is designed to convert images to vector graphics. After vectorization, all graphical elements, such as geometric shapes, Bezier curves, splines, paths, and lines, are saved as vector images in SVG files. Image Vectorizer supports a variety of bitmap formats, including JPEG, PNG, BMP, ICO, GIF, and TIFF. Easily manipulate your vectorized SVG image with interactive controls linked to vectorization options. Convert image to SVG and get scalable, crisp vector art today!
Please refer to the How to use Image Vectorizer section to learn more about how to use the Image Vectorizer and what Quantization and Vectorization options mean.
How to vectorize an image in Python
Aspose.SVG for Python via .NET API provides classes and methods that allow you to convert image to SVG and work with various options for pre-processing images before saving them in vector format. You can control the following vectorization parameters: trace_smoother
, error_threshold
, max_iterations
, path_builder
, stencil
, etc. In the following code example, we convert image to SVG with explicitly specified vectorization options:
Convert Image to SVG in Python
import os
from aspose.svg import *
from aspose.svg.drawing import *
from aspose.svg.rendering.image import *
from aspose.svg.imagevectorization import *
# Setup directories
input_folder = "data/"
output_folder = "output/"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Configuration for image vectorization
path_builder = BezierPathBuilder()
path_builder.trace_smoother = ImageTraceSmoother(2) # Example severity level
path_builder.error_threshold = 30.0 # Example threshold
path_builder.max_iterations = 30 # Example max iterations
vectorizer = ImageVectorizer()
vectorizer.configuration.path_builder = path_builder
vectorizer.configuration.colors_limit = 25 # Example color limit
vectorizer.configuration.line_width = 1.0 # Example line width
# Vectorize an image
src_file = "input.png" # Specify the sourse image file
with vectorizer.vectorize(os.path.join(input_folder, src_file)) as document:
output_file = os.path.join(output_folder, "output.svg") # Specify the output SVG file
document.save(output_file)
print(f"Vectorized image saved to {output_file}")
Step-by-Step Image Vectorization with Python
To vectorize an image with Aspose.SVG Python library, you should follow a few steps:
- Import the necessary modules and classes from the
aspose.svg
,aspose.svg.imagevectorization
, and other necessary packages. - Create an instance of the
BezierPathBuilder
class to define the path-building strategy.
- Set the
trace_smoother
attribute using an instance of ImageTraceSmoother with aseverity
level of 2. Theseverity
level indicates the extent of smoothing applied to the paths. - Set the value for the
error_threshold
property ofBezierPathBuilder
. This parameter defines the acceptable error margin for approximating the bitmap shapes with vector paths. Lower values result in more accurate approximations, while higher values allow for more simplification. - Specify the value for the
max_iterations
property of theBezierPathBuilder
object. This setting determines the maximum number of iterations allowed for the path optimization algorithm during vectorization, which is how many times the algorithm will attempt to improve the path accuracy.
- Set the
- Create an instance of the
ImageVectorizer
class. The
ImageVectorizer
is the main class used to convert images to vector graphics.- Assign the
path_builder
configuration (defined earlier) to thevectorizer
object. This means the vectorizer will use the settings specified in thepath_builder
for path creation and optimization. - Set the
colors_limit
property of the ImageVectorizerConfiguration class. This parameter determines the maximum number of colors that will be used in the vectorized image. This affects how the vectorizer handles color simplification and reduces the number of unique colors in the output. - Specify the
line_width
property of theImageVectorizerConfiguration
class. This setting determines the width of the vector paths in the resulting SVG file. It controls the stroke width of paths created during vectorization.
- Assign the
- Use the vectorize() method to vectorize an image from the specified file.
- Save the vectorized image as an SVG file using the
save()
method on the resulting
SVGDocument
.
Image Vectorization is the process of converting raster images into vector graphics, such as Bezier curves, paths, and lines. In the documentation chapter Image and Text Vectorization , you will find detailed information about image vectorization, including a description of the process and available options. Learn how to vectorize raster images like PNG, JPG, BMP, TIFF, GIF, and ICO into an SVG document. The chapter also includes several examples demonstrating the functionalities of the Aspose.Svg.ImageVectorization namespace and the effects of configuration properties on the results.
Aspose.SVG offers a Free Online Image Vectorizer that allows you to convert JPG, PNG, BMP, TIFF, ICO, and GIF bitmap images into vector graphics. With this application, you can apply various options to achieve the perfect result. Save time and experience the benefits of vector graphics by trying out this Image Vectorizer today!
FAQ
Aspose.SVG allows you to convert Image to SVG in any way – online or programmatically. For example, you can vectorize Image in real-time using Online Image Vectorizer that converts your images quickly and with high quality. On other hand, you can use Aspose.SVG to vectorize Image programmatically.
The process of image conversion from raster to vector is called image vectorization. It is a complex computational process that converts a bitmap image made up of pixels into a vector image made up of lines, curves, and other geometric shapes.
Vector images are essential for any professional who works with drawings, graphics, or sketches. Many designers, printers, architects, and artists often choose to work with sketches or drawings as vector images. And they have good reasons! Unlike bitmap graphics, vector arts are resizable and scalable without losing any quality, and they will still display just as clearly. Also, vectorization can be used to update or recover images.
Vector images are what you need to produce banners, logos, maps, signs, embroideries, engravings, graphs, blueprints, etc.
The vectorization process uses color image quantization – a technique that reduces the number of different colors used in an image, intending that the new image should be as visually similar as possible to the original one. Then, the Vectorizer replaces all the same colored spots or pixels with geometric shapes or curves using a set of contour tracing algorithms. The final step is to convert the tracing points to SVG path lines and Bezier curves and add them to the SVG document.
Get Started with Python API
If you want to develop scalable vector graphics and their applications, install our flexible, high-speed Aspose.SVG for Python via .NET API. pip
is the easiest way to download and install Aspose.SVG for Python via .NET API. To do this, run the following command:
pip install aspose-svg-net
For more details about Python library installation and system requirements, please refer to Aspose.SVG Documentation.