Image Vectorization – Vectorize an image with Cloud SDK
Image Vectorization
Aspose.HTML Cloud supports the vectorization of the following image formats: PNG, JPEG, BMP, TIFF, and GIF. By using our image vectorization API, you can convert your raster images into scalable vector graphics, preserving their quality and making them suitable for high-resolution displays and prints. Whether you’re working with photos, logos, or illustrations, our API ensures precise and efficient vectorization, integrating seamlessly into your workflow.
Image Vectorization is the process of converting raster images, composed of pixels, into vector graphics, composed of paths defined by mathematical equations. This conversion enables images to be scaled infinitely without losing quality, making them ideal for various applications such as graphic design, printing, and digital artwork.
The article explains how to vectorize images using our API and client SDKs in a set of code examples. SDKs are available in PHP, C++, Python, C#, Ruby, Java, Android, Swift, and Node.js. We will consider various scenarios of how to vectorize an image:
- Vectorizing a local raster image file and saving the result in your local file system. In this scenario, we will explore both image vectorizations with default parameters and with explicitly specified parameters.
- Vectorizing raster images in cloud storage with default vectorization parameters.
Examples of Image Vectorization
A common use case for our API functions is image processing and vectorization. The API enables you to retrieve a raster image (such as PNG, JPG, JPEG, TIFF, GIF, or BMP) from storage by its name or from a local file on your drive, convert it to a vector format, and save it back to storage or your local drive. The following code examples demonstrate how to vectorize an image using various SDKs and cURL for different scenarios.
Asynchronous vs Synchronous Vectorization
There are two general approaches for vectorizing Image to SVG. The asynchronous approach involves submitting a vectorization request, receiving an operation ID, and then checking the status of the vectorization until it is completed. Once the status is completed, the resulting file can be downloaded using the provided download link. The synchronous approach, on the other hand, processes the vectorization in a single operation. The API waits for the vectorization to complete and directly returns the PDF in the response, eliminating the need to check statuses and perform separate download steps. Both methods support the same conversion options.
Asynchronous Vectorization
The asynchronous vectorization approach allows for the submission of a vectorization request, where the server processes the vectorization in the background. Once the request is submitted, an operation ID is returned, which can be used to check the status of the vectorization. Upon completion, the resulting file can be downloaded using the provided ID and download link. Below is an example of how to initiate an asynchronous conversion request.
Submit vectorization request
1curl -X POST -v \
2 "https://api.aspose.cloud/v4.0/html/conversion/jpeg-svg" \
3 -d "{'InputPath': 'https://example.com/example.jpeg'}" \
4 -H "Content-Type: application/json" \
5 -H "Authorization:Bearer <JWT_token>"
Result:
1{
2 "file": null,
3 "code": 200,
4 "id": "CN-a902f0ab-6d67-4847-93b3-9d90826f7ead",
5 "status": "running",
6 "description": null,
7 "links": {
8 "self": "/v4.0/html/conversion/CN-a902f0ab-6d67-4847-93b3-9d90826f7ead",
9 "download": null
10 }
11}
Checking vectorization Status:
To check the status of the vectorization, use the id returned in the previous response:
1curl -X GET "https://api.aspose.cloud/v4.0/html/conversion/CN-a902f0ab-6d67-4847-93b3-9d90826f7ead" \
2 -H "Content-Type: application/json" \
3 -H "Authorization:Bearer <JWT_token>"
Result:
1{
2 "file": "76482e8b-8d2e-465b-8407-5c7f6e93e614/example.svg",
3 "code": 200,
4 "id": "CN-a902f0ab-6d67-4847-93b3-9d90826f7ead",
5 "status": "completed",
6 "links": {
7 "self": "/v4.0/html/conversion/CN-a902f0ab-6d67-4847-93b3-9d90826f7ead",
8 "download": "v4.0/html/file?path=76482e8b-8d2e-465b-8407-5c7f6e93e614/example.svg"
9 }
10}
Possible Statuses:
• completed
• pending
• running
• faulted
• canceled
Downloading the Vectorization Result:
If the status is completed, the result can be downloaded using the id:
1curl -X GET "https://api.aspose.cloud/v4.0/html/conversion/CN-662a4f3f-44a3-43ed-8c23-5c7b2a052c02/download" \
2 -H "Authorization:Bearer <JWT_token>"
The REST API will return the file as a response. This response will include the Content-Type set as application/octet-stream and the file will be sent as a binary stream for download. This is equivalent to:
1Content-Type: application/octet-stream
2Content-Disposition: attachment; filename="example.svg"
Synchronous Vectorization
For smaller files, synchronous vectorization can be used to eliminate the need to check statuses and download the result in separate calls. The entire process is handled within one call:
1curl -X POST "https://api.aspose.cloud/v4.0/html/conversion/jpeg-svg/sync" \
2 -H "Content-Type: application/json" \
3 -H "Authorization:Bearer <JWT_token>" \
4 -d '{"InputPath": "https://example.com/example.jpeg"}'
Result:
1Content-Type: application/octet-stream
2Content-Disposition: attachment; filename="example.svg"
Vectorize an Image with Default Save Options
Here’s an example of vectorizing a local raster image file and saving the result to the local file system using default vectorization options.
Vectorization Options
Our API provides several options to fine-tune the vectorization process. Below is a description of the available vectorization options:
- ErrorThreshold: The acceptable error threshold for the vectorization process. This value controls the accuracy of the vectorization. A lower value results in higher accuracy but may require more processing time. Default value is 30.
- MaxIterations: The maximum number of iterations allowed during the vectorization process. This setting limits the processing time and complexity. Default value is 30.
- ColorLimit: The maximum number of colors allowed in the vectorized output. This option helps control the complexity and size of the resulting vector file. Default value is 25.
- LineWidth: The width of the lines in the resulting vector image. This setting controls the visual appearance of the vectorized output. Default value is 1.
Convert PNG to SVG – Vectorize PNG in C#
Here is a C# example of how to use these options to vectorize PNG and save the result as an SVG file:
1// Initialize SDK API
2var api = new HtmlApi("CLIENT_ID", "CLIENT_SECRET").VectorizationApi;
3var options = new VectorizationOptions
4 {
5 ErrorThreshold = 30,
6 MaxIterations = 30,
7 ColorLimit = 25,
8 LineWidth = 1
9 };
10
11// Convert PNG to SVG
12var result = await api.VectorizeAsync("input.png", "output.svg", options);
This example demonstrates how to initialize the API with client credentials, set the vectorization options, and convert PNG to SVG using the specified options.
Vectorize PNG with Vectorization Options – cURL Example
Below is an example of how to use cURL to vectorize an image with specified options:
1curl -X POST -v \
2 "https://api.aspose.cloud/v4.0/html/conversion/png-svg" \
3 -d '{
4 "InputPath": "/input.png",
5 "OutputFile": "output.svg",
6 "Options": {
7 "error_threshold": 30,
8 "max_iterations": 30,
9 "colors_limit": 25,
10 "line_width": 1
11 }
12 }' \
13 -H "Content-Type: application/json" \
14 -H "Authorization: Bearer <JWT_token>"
See Also
- The Available SDKs article introduces you to Aspose.HTML Cloud’s ability to use SDKs in various programming languages, such as C#, Java, Python, Ruby, PHP, Node.js, Swift, Android, and C++.
- The SDK Conversion Options article describes a set of classes that represent options for converting a source HTML-based document to PDF, XPS, and image.
- Aspose.HTML Cloud API you can call directly from your browser by accessing the API Reference.
- You can try a free online PNG to SVG Converter that swiftly converts PNG to SVG with high quality. Just upload your files, convert them, and get the result in a few seconds!
- Free online Image Vectorizer quickly and efficiently converts your raster images to vector SVG format. Using this application, you can apply a set of options to get the perfect result. Save your time and use this free Image Vectorizer to get all the benefits of vector graphics!