ColorPaletteHelper Class

Summary: Helper class for color palettes manipulation.

Module: aspose.imaging

Full Name: aspose.imaging.ColorPaletteHelper

Methods

NameDescription
create_4_bit()Creates the 4 bit color palette.
create_4_bit_grayscale(min_is_white)Creates the 4 bit grayscale palette.
create_8_bit()Creates the 8 bit color palette.
create_8_bit_grayscale(min_is_white)Creates the 8 bit grayscale palette.
create_monochrome()Creates a monochrome color palette containing 2 colors only.
get_close_image_palette(image, dest_bounds, entries_count)Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.
get_close_image_palette(image, dest_bounds, entries_count, use_image_palette)Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.
get_close_image_palette(image, dest_bounds, entries_count, use_image_palette, alpha_blend_in_color)Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.
get_close_image_palette(image, dest_bounds, entries_count, use_image_palette, alpha_blend_in_color, keep_transparency)Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.
get_close_image_palette(image, entries_count)Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.
get_close_image_palette(image, entries_count, palette_mining_method)Gets color palette from raster image (palletizes image) in case the image does not have one. Palette is about to be optimized for better indexed image quality or taken “AS IS” when PaletteMiningMethod.UseCurrentPalette is used.
get_close_image_palette_by_method(image, entries_count, palette_mining_method)Gets color palette from raster image (palletizes image) in case the image does not have one. Palette is about to be optimized for better indexed image quality or taken “AS IS” when PaletteMiningMethod.UseCurrentPalette is used.
get_close_image_palette_by_rect(image, dest_bounds, entries_count)Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.
get_close_transparent_image_palette(image, entries_count)Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.
get_downscale_palette(image)Get 256 color palette, composed from upper bits of initial image color values.
get_uniform_color_palette(image)Get uniform 256 color palette.
has_transparent_colors(palette)Determines whether the specified palette has transparent colors.

Method: create_4_bit() [static]

 create_4_bit() 

Creates the 4 bit color palette.

Returns

TypeDescription
IColorPaletteThe 4 bit color palette.

Method: create_4_bit_grayscale(min_is_white) [static]

 create_4_bit_grayscale(min_is_white) 

Creates the 4 bit grayscale palette.

Parameters:

ParameterTypeDescription
min_is_whiteboolif set to true the palette starts with white color, otherwise it starts with black color.

Returns

TypeDescription
IColorPaletteThe 4 bit grayscale palette.

Method: create_8_bit() [static]

 create_8_bit() 

Creates the 8 bit color palette.

Returns

TypeDescription
IColorPaletteThe 8 bit color palette.

Method: create_8_bit_grayscale(min_is_white) [static]

 create_8_bit_grayscale(min_is_white) 

Creates the 8 bit grayscale palette.

Parameters:

ParameterTypeDescription
min_is_whiteboolif set to true the palette starts with white color, otherwise it starts with black color.

Returns

TypeDescription
IColorPaletteThe 8 bit grayscale palette.

Method: create_monochrome() [static]

 create_monochrome() 

Creates a monochrome color palette containing 2 colors only.

Returns

TypeDescription
IColorPaletteColor palette for monochrome images.

Method: get_close_image_palette(image, dest_bounds, entries_count) [static]

 get_close_image_palette(image, dest_bounds, entries_count) 

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
dest_boundsRectangleThe destination image bounds.
entries_countintThe desired entries count.

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Method: get_close_image_palette(image, dest_bounds, entries_count, use_image_palette) [static]

 get_close_image_palette(image, dest_bounds, entries_count, use_image_palette) 

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
dest_boundsRectangleThe destination image bounds.
entries_countintThe desired entries count.
use_image_paletteboolIf set, it will use its own image palette if available

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Method: get_close_image_palette(image, dest_bounds, entries_count, use_image_palette, alpha_blend_in_color) [static]

 get_close_image_palette(image, dest_bounds, entries_count, use_image_palette, alpha_blend_in_color) 

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
dest_boundsRectangleThe destination image bounds.
entries_countintThe desired entries count.
use_image_paletteboolIf set, it will use its own image palette if available
alpha_blend_in_colorColorThe color that should be used as a background color for semi-transparent alpha replacement.

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Method: get_close_image_palette(image, dest_bounds, entries_count, use_image_palette, alpha_blend_in_color, keep_transparency) [static]

 get_close_image_palette(image, dest_bounds, entries_count, use_image_palette, alpha_blend_in_color, keep_transparency) 

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
dest_boundsRectangleThe destination image bounds.
entries_countintThe desired entries count.
use_image_paletteboolIf set, it will use its own image palette if available
alpha_blend_in_colorColorThe color that should be used as a background color for semi-transparent alpha replacement.
keep_transparencyboolIf set, it will consider alpha channel bits of the image colors.

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Method: get_close_image_palette(image, entries_count) [static]

 get_close_image_palette(image, entries_count) 

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
entries_countintThe desired entries count.

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

See also:

Example # 1: The following example shows how to set a palette to a BMP image to reduce its…

Example # 2: The following example loads a BMP image and saves it back to BMP using variou…

Method: get_close_image_palette(image, entries_count, palette_mining_method) [static]

 get_close_image_palette(image, entries_count, palette_mining_method) 

Gets color palette from raster image (palletizes image) in case the image does not have one. Palette is about to be optimized for better indexed image quality or taken “AS IS” when PaletteMiningMethod.UseCurrentPalette is used.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
entries_countintThe desired entries count.
palette_mining_methodPaletteMiningMethodThe palette mining method.

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

See also:

Example # 1: The following example shows how to compress a PNG image, using indexed color …

Method: get_close_image_palette_by_method(image, entries_count, palette_mining_method) [static]

 get_close_image_palette_by_method(image, entries_count, palette_mining_method) 

Gets color palette from raster image (palletizes image) in case the image does not have one. Palette is about to be optimized for better indexed image quality or taken “AS IS” when PaletteMiningMethod.UseCurrentPalette is used.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
entries_countintThe desired entries count.
palette_mining_methodPaletteMiningMethodThe palette mining method.

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Method: get_close_image_palette_by_rect(image, dest_bounds, entries_count) [static]

 get_close_image_palette_by_rect(image, dest_bounds, entries_count) 

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
dest_boundsRectangleThe destination image bounds.
entries_countintThe desired entries count.

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Method: get_close_transparent_image_palette(image, entries_count) [static]

 get_close_transparent_image_palette(image, entries_count) 

Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

Parameters:

ParameterTypeDescription
imageRasterImageThe raster image.
entries_countintThe desired entries count.

Returns

TypeDescription
IColorPaletteThe color palette which starts with the most frequent colors from the image and contains entriesCount entries.

Method: get_downscale_palette(image) [static]

 get_downscale_palette(image) 

Get 256 color palette, composed from upper bits of initial image color values.

Parameters:

ParameterTypeDescription
imageRasterImageThe image.

Returns

TypeDescription
ColorPaletteThe ColorPalette.

Method: get_uniform_color_palette(image) [static]

 get_uniform_color_palette(image) 

Get uniform 256 color palette.

Parameters:

ParameterTypeDescription
imageRasterImageThe image.

Returns

TypeDescription
ColorPaletteThe ColorPalette.

Method: has_transparent_colors(palette) [static]

 has_transparent_colors(palette) 

Determines whether the specified palette has transparent colors.

Parameters:

ParameterTypeDescription
paletteIColorPaletteThe palette.

Returns

TypeDescription
booltrue if the specified palette has transparent colors; otherwise, false.

Examples

The following example shows how to set a palette to a BMP image to reduce its output size.


from aspose.pycore import as_of
from aspose.imaging import Point, Color, Graphics, ColorPaletteHelper
from aspose.imaging.brushes import LinearGradientBrush
from aspose.imaging.fileformats.bmp import BmpImage
from aspose.imaging.imageoptions import BmpOptions
from os.path import join as path_join

# Create a BMP image 100 x 100 px.
with BmpImage(100, 100) as bmpImage:
	# The linear gradient from the left-top to the right-bottom corner of the image.
	brush = LinearGradientBrush(Point(0, 0), Point(bmpImage.width, bmpImage.height),
								Color.red,
								Color.green)
	# Fill the entire image with the linear gradient brush.
	gr = Graphics(bmpImage)
	gr.fill_rectangle(brush, bmpImage.bounds)
	# Get the closest 8-bit color palette which covers as many pixels as possible, so that a palettized image
	# is almost visually indistinguishable from a bmp without palette
	palette = ColorPaletteHelper.get_close_image_palette(bmpImage, 256)
	# 8-bit palette contains at most 256 colors.
	saveOptions = BmpOptions()
	saveOptions.palette = palette
	saveOptions.bits_per_pixel = 8
	
	with stream_ext.create_memory_stream() as stream:
		bmpImage.save(stream, saveOptions)
		print(f"The size of image with palette is {stream.tell()} bytes.")
		stream.seek(0)
		bmpImage.save(stream)
		print(f"The size of image without palette is {stream.tell()} bytes.")

# The output looks like this:
# The size of image with palette is 11078 bytes.
# The size of image without palette is 40054 bytes.

The following example shows how to compress a PNG image, using indexed color with best fit palette


from aspose.pycore import as_of
from aspose.imaging import Image, ColorPaletteHelper, RasterImage, PaletteMiningMethod
from aspose.imaging.fileformats.png import PngColorType

# Loads png image        
sourceFilePath = "OriginalRings.png"
outputFilePath = "OriginalRingsOutput.png"
with Image.load(sourceFilePath) as image:
	png_options = PngOptions()
	png_options.progressive = True
	# Use indexed color type
	png_options.color_type = PngColorType.INDEXED_COLOR
	# Use maximal compression
	png_options.compression_level = 9
	# Get the closest 8-bit color palette, covering as many pixels as possible, so that an image
	# with palette is almost visually indistinguishable from an image without a palette.
	png_options.palette = ColorPaletteHelper.get_close_image_palette(
						as_of(image, RasterImage), 256, 
						PaletteMiningMethod.HISTOGRAM)
		 
	image.save(outputFilePath, png_options);
}
# The output file size should be significantly reduced

The following example loads a BMP image and saves it back to BMP using various save options.

from aspose.imaging import Image, RasterImage, ColorPaletteHelper, ResolutionSetting
from aspose.imaging.imageoptions import BmpOptions
from aspose.imaging.fileformats.bmp import BitmapCompression
import os
import aspose.pycore as aspycore

directory = "c:\\temp\\"

with Image.load(os.path.join(directory, "sample.bmp")) as image:
	
	rasterImage = aspycore.as_of(image, RasterImage)

	# Create BmpOptions
	saveOptions = BmpOptions()

	# Use 8 bits per pixel to reduce the size of the output image.
	saveOptions.bits_per_pixel = 8

	# Set the closest 8-bit color palette which covers the maximal number of image pixels, so that a palettized image
	# is almost visually indistinguishable from a non-palletized one.
	saveOptions.palette = ColorPaletteHelper.get_close_image_palette(rasterImage, 256)

	# Save without compression.
	# You can also use RLE-8 compression to reduce the size of the output image.
	saveOptions.compression = BitmapCompression.RGB

	# Set the horizontal and vertical resolution to 96 dpi.
	saveOptions.resolution_settings = ResolutionSetting(96.0, 96.0)

	image.save(os.path.join(directory, "sample.bmpoptions.bmp"), saveOptions)