ImageVectorizer and color palette

Hello Aspose community,

I’m trying to vectorize some png images using Aspose but I want to define a target color palette.
Let’s say a shade of oranges.
I want my result image to use only colors from the palette.

My project targets .Net9 and references Aspose.SVG 25.7.0.
I use the ImageVectorizer and its Configuration.

I searched the reference doc and the forum but found no palette option, only ColorsLimit.
Yet, the online vectorizer tool allows for palette configuration : https://blog.aspose.com/svg/vectorize-image-online-with-free-vectorizer/

Is there a way to define a palette ?
Maybe I should preprocess the image myself, or postprocess the output svg ?

Thank you for your king help,
Gegesse

@Gegesse

To define a target color palette when vectorizing PNG images using Aspose.SVG, you currently cannot directly specify a color palette in the ImageVectorizer configuration. The available option is ColorsLimit, which restricts the number of colors in the output image but does not allow for a specific palette.

Cause: The limitation arises because the ImageVectorizer does not support direct color palette specification, which can hinder achieving a specific visual outcome.

Solution: To work around this limitation, you can follow these steps:

  1. Preprocess the Image: Convert the image to your desired color palette by replacing colors in the image with the closest colors from your target palette.

  2. Postprocess the Output SVG: After vectorization, modify the resulting SVG to replace colors with those from your palette by parsing the SVG and changing the fill and stroke attributes.

Next Steps: Here’s a minimal code example to illustrate how you might preprocess the image:

using Aspose.Svg;
using Aspose.Svg.Drawing;
using Aspose.Svg.ImageVectorization;

// Load your PNG image
using (var image = Image.Load("input.png"))
{
    // Preprocess the image to match your color palette
    // (Implement your color replacement logic here)

    // Vectorize the image
    var vectorizer = new ImageVectorizer();
    var svg = vectorizer.Vectorize(image);

    // Save the SVG
    svg.Save("output.svg");
}

For the color replacement logic, you will need to implement a method that maps the original colors to your target palette.

If you require further assistance or specific examples on how to implement the color replacement, please let me know! I am here to help you achieve your desired outcome.

Thanks for your answer.

I indeed preprocess the image using System.Drawing, wich was quite simple to my surprise.

I setup my target palette in a Color array.
Loop each pixel in Bitmap and colors in palette to find the shortest color distance.

Could share the code if anyone is interested.

@Gegesse

It is nice to know that you have been able to achieve what you required. You can off course share the sample code as it could definitely help others who may be interested in this kind of functionality.