PngOptions Class

Summary: Create high-quality Portable Network Graphics (PNG) raster images effortlessly
with our API, offering customizable options for compression levels,
bits per pixel depths, and alpha bits. Seamlessly process XMP metadata containers,
ensuring comprehensive image metadata management, and empowering you to tailor
PNG images to your exact specifications with ease.

Module: aspose.imaging.imageoptions

Full Name: aspose.imaging.imageoptions.PngOptions

Inheritance: IHasXmpData, IHasMetadata, ImageOptionsBase

Constructors

NameDescription
PngOptions()Initializes a new instance of the PngOptions class.
PngOptions(png_options)Initializes a new instance of the PngOptions class.

Properties

NameTypeAccessDescription
DEFAULT_COMPRESSION_LEVEL [static]intrThe default compression level.
bit_depthbyter/wGets or sets the bit depth values in range of 1, 2, 4, 8, 16.


Mind the next limits:


PngColorType.GRAYSCALE, PngColorType.INDEXED_COLOR support bit depth of 1, 2, 4, 8.


PngColorType.GRAYSCALE_WITH_ALPHA supports bit depth of 8.


PngColorType.TRUECOLOR, PngColorType.TRUECOLOR_WITH_ALPHA support bit depth of 8, 16.

buffer_size_hintintr/wGets or sets the buffer size hint which is defined max allowed size for all internal buffers.
color_typePngColorTyper/wGets or sets the type of the color.
compression_levelintr/wGets or sets the PngImage compression level in the range of 0-9. The higher the value - the more efficient the compression.
disposedboolrGets a value indicating whether this instance is disposed.
filter_typePngFilterTyper/wGets or sets the filter type used during png file save process.
full_frameboolr/wGets or sets a value indicating whether [full frame].
keep_metadataboolr/wGets a value whether to keep original image metadata on export.
multi_page_optionsMultiPageOptionsr/wThe multipage options
paletteIColorPaletter/wGets or sets the color palette.
progressiveboolr/wGets or sets a value indicating whether a PngImage is progressive.
resolution_settingsResolutionSettingr/wGets or sets the resolution settings.
sourceSourcer/wGets or sets the source to create image in.
vector_rasterization_optionsVectorRasterizationOptionsr/wGets or sets the vector rasterization options.
xmp_dataXmpPacketWrapperr/wGets or sets the XMP metadata container.

Methods

NameDescription
clone()Creates a memberwise clone of this instance.

Constructor: PngOptions()

 PngOptions() 

Initializes a new instance of the PngOptions class.

Constructor: PngOptions(png_options)

 PngOptions(png_options) 

Initializes a new instance of the PngOptions class.

Parameters:

ParameterTypeDescription
png_optionsPngOptionsThe PNG options.

Property: color_type

Gets or sets the type of the color.

See also:

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

Property: compression_level

Gets or sets the PngImage compression level in the range of 0-9. The higher the value - the more efficient the compression.

See also:

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

Property: progressive

Gets or sets a value indicating whether a PngImage is progressive.

See also:

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

Method: clone()

 clone() 

Creates a memberwise clone of this instance.

Returns

TypeDescription
ImageOptionsBaseA memberwise clone of this instance.

Examples

This example uses Graphics class to create primitive shapes on the Image surface. To demonstrate the operation, the example creates a new Image in PNG format and draw primitive shapes on Image surface using Draw methods exposed by Graphics class


from aspose.imaging import Image, RotateFlipType, Graphics, Color, Pen, Rectangle, Point, Size,\
	Font, PointF
from aspose.imaging.brushes import SolidBrush
from aspose.imaging.imageoptions import PngOptions
from aspose.imaging.fileformats.psd import CompressionMethod, ColorModes
from aspose.imaging.sources import StreamSource

from os.path import join as path_join

#Creates an instance of file stream
with open(r"C:\temp\output.png", "w+b") as stream:
	#Create an instance of PngOptions and set its various properties
	pngOptions = PngOptions()
	#Set the Source for PngOptions
	pngOptions.source = StreamSource(stream)
	#Create an instance of Image 
	with Image.create(pngOptions, 500, 500) as image:
		#Create and initialize an instance of Graphics class
		graphics = Graphics(image)
		#Clear Graphics surface
		graphics.clear(Color.wheat);
		#Draw an Arc by specifying the Pen object having Black color, 
		#a Rectangle surrounding the Arc, Start Angle and Sweep Angle
		graphics.draw_arc(Pen(Color.black, 2.0), Rectangle(200, 200, 100, 200), 0, 300)
		#Draw a Bezier by specifying the Pen object having Blue color and co-ordinate Points.
		graphics.draw_bezier(Pen(Color.blue, 2.0), Point(250, 100), Point(300, 30), Point(450, 100), Point(235, 25))
		#Draw a Curve by specifying the Pen object having Green color and an array of Points
		graphics.draw_curve(Pen(Color.green, 2.0), [Point(100, 200), Point(100, 350), Point(200, 450)])
		#Draw an Ellipse using the Pen object and a surrounding Rectangle
		graphics.draw_ellipse(Pen(Color.yellow, 2.0), Rectangle(300, 300, 100, 100))
		#Draw a Line 
		graphics.draw_line(Pen(Color.violet, 2.0), Point(100, 100), Point(200, 200))
		#Draw a Pie segment
		graphics.draw_pie(Pen(Color.silver, 2.0), Rectangle(Point(200, 20), Size(200, 200)), 0, 45);
		#Draw a Polygon by specifying the Pen object having Red color and an array of Points
		graphics.draw_polygon(Pen(Color.red, 2.0), [Point(20, 100), Point(20, 200), Point(220, 20)])
		#Draw a Rectangle
		graphics.draw_rectangle(Pen(Color.orange, 2.0), Rectangle(Point(250, 250), Size(100, 100)))
		#Create a SolidBrush object and set its various properties
		brush = SolidBrush()
		brush.color = Color.purple
		#Draw a String using the SolidBrush object and Font, at specific Point
		graphics.draw_string("This image is created by Aspose.Imaging API", Font("Times New Roman", 16),
							 brush, PointF(50.0, 400.0))
		# save all changes.
		image.save();

This example demonstrates the use of different classes from imageoptions package for export purposes. A gif image is loaded as an instance of Image and then exported out to several formats.


from aspose.imaging import Image
from aspose.imaging.imageoptions import BmpOptions, JpegOptions, PngOptions, TiffOptions
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat
from os.path import join as path_join

directory = "c:\\temp\\"
#Load an existing gif image as an instance of Image class
with Image.load(path_join(directory, "sample.gif")) as image:
	# Export to BMP file format using the default options
	image.save(path_join(directory, "output.bmp"), BmpOptions())
	# Export to JPEG file format using the default options
	image.save(path_join(directory, "output.jpg"), JpegOptions())
	# Export to PNG file format using the default options
	image.save(path_join(directory, "output.png"), PngOptions())
	# Export to TIFF file format using the default options
	image.save(path_join(directory, "output.tif"), TiffOptions(TiffExpectedFormat.DEFAULT))

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