Is it possible to perform color reduction by specifying specific colors instead of just setting the number of colors? I would like to specify certain colors (from a fixed palette of 2 to 37 colors) during the reduction process. Additionally, is there an option to enable or disable dithering?
According to the documentation on the official website: RasterImage.Dither | Aspose.Imaging for .NET API Reference
it seems that using specific colors for color reduction may not be supported. It mentions: “Perform threshold dithering using a 4-bit color palette which contains 16 colors. The more bits specified, the higher the quality and the larger the output image size. Note that only 1-bit, 4-bit, and 8-bit palettes are supported at the moment.”
Could you confirm if this is correct?
Hello @yamashin
You can use public abstract void Dither(DitheringMethod ditheringMethod, int bitsCount, IColorPalette customPalette) method to specify certain colors.
The resulting code will look like this:
using (var image = Aspose.Imaging.Image.Load("source.png"))
{
Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;
var palette = new ColorPalette(new Color[] {
// here goes the color list
});
rasterImage.Dither(Aspose.Imaging.DitheringMethod.FloydSteinbergDithering, 4, palette);
image.Save("output.png");
}
Please also pay attention that you can use this class
to simplify the way palette is created.
1 Like
I was able to successfully perform color reduction with specified colors. Thank you very much for your assistance.
The reason I couldn’t initially specify colors was because I had set the Dither
method to DitheringMethod.ThresholdDithering
. By switching to DitheringMethod.FloydSteinbergDithering
, I was able to achieve the desired result.
I have two additional questions:
- Is it possible to perform color reduction without dithering?
- Can transparency be preserved when saving an image after color reduction? The input image has a transparent background, but when I perform the conversion with the advised approach, the transparent areas turn black. When I add transparency to the custom color palette, it seems to preserve transparency during conversion, but the output image still saves those areas as black. Would the only option be to preserve transparency before conversion and reapply it afterward?
Thank you again for your help with these questions.
@yamashin
You can save png with palette added to PngOptions without disering. Like this:
using (var image = Aspose.Imaging.Image.Load("source.png"))
{
Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;
var palette = ColorPaletteHelper.GetCloseTransparentImagePalette(rasterImage, 32);
image.Save("output.png", new PngOptions()
{
ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.IndexedColor,
Palette = palette
});
}
This will save the image with closest color from palette.
Taking about transparency. By default png images are saved without transparency. You may add it by using ColorType in PngOptions.
This is 32bit(4 channels by 8 bits) save:
image.Save("output.png", new PngOptions()
{
ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha,
Palette = palette
});
And this is saving with indexed palette with transparent colors:
image.Save("output.png", new PngOptions()
{
ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.IndexedColor,
Palette = palette
});