BmpOptions Class

Summary: The API for BMP and DIB raster image format creation options provides developers
with a versatile toolset for generating custom Bitmap (BMP) and Device
Independent Bitmap (DIB) images. With this API, you can precisely define
image characteristics such as bits per pixel, compression level and compression
type, tailoring the output to meet specific requirements. This feature-rich
API empowers developers to create high-quality, customized raster images
with ease and flexibility for diverse applications.

Module: aspose.imaging.imageoptions

Full Name: aspose.imaging.imageoptions.BmpOptions

Inheritance: ImageOptionsBase

Aspose.Imaging Version: 24.5.0

Constructors

NameDescription
BmpOptions()Initializes a new instance of the BmpOptions class.
BmpOptions(bmp_options)Initializes a new instance of the BmpOptions class.

Properties

NameTypeAccessDescription
bits_per_pixelintr/wGets or sets the image bits per pixel count.
buffer_size_hintintr/wGets or sets the buffer size hint which is defined max allowed size for all internal buffers.
compressionBitmapCompressionr/wGets or sets the compression type. The default compression type is BitmapCompression.BITFIELDS, that allows saving a BmpImage with transparency.
disposedboolrGets a value indicating whether this instance is disposed.
full_frameboolr/wGets or sets a value indicating whether [full frame].
multi_page_optionsMultiPageOptionsr/wThe multipage options
paletteIColorPaletter/wGets or sets the color palette.
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()Clones this instance.

Constructor: BmpOptions()

 BmpOptions() 

Initializes a new instance of the BmpOptions class.

See also:

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

Constructor: BmpOptions(bmp_options)

 BmpOptions(bmp_options) 

Initializes a new instance of the BmpOptions class.

Parameters:

ParameterTypeDescription
bmp_optionsBmpOptionsThe BMP options.

Property: bits_per_pixel

Gets or sets the image bits per pixel count.

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…

Property: compression

Gets or sets the compression type. The default compression type is BitmapCompression.BITFIELDS, that allows saving a BmpImage with transparency.

See also:

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

Example # 2: The example shows how to export a BMP from a PNG file while keeping the alpha…

Example # 3: The example shows how to export a BMP with the RGB compression type.

Method: clone()

 clone() 

Clones this instance.

Returns

TypeDescription
ImageOptionsBaseReturns shallow copy of this instance

Examples

This example creates a new Image file at some disk location as specified by Source property of the BmpOptions instance. Several properties for BmpOptions instance are set before creating the actual image. Especially the Source property, that refers to the actual disk location in this case.


from aspose.imaging import Image
from aspose.imaging.imageoptions import BmpOptions
from aspose.imaging.sources import FileCreateSource

#Create an instance of `BmpOptions` and set its various properties
with BmpOptions() as bmp_options:
	bmp_options.bits_per_pixel = 24

	#Create an instance of `FileCreateSource` and assign it as `source` for the instance of `BmpOptions`
	#Second `Boolean` parameter determines if the file to be created is_temporal or not
	bmp_options.source = FileCreateSource(r"C:\temp\output.bmp", False)

	#Create an instance of Image and initialize it with instance of BmpOptions by calling Create method
	with Image.create(bmp_options, 500, 500) as image:
		#do some image processing
		# 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 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 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)

The example shows how to export a BMP from a PNG file while keeping the alpha channel, save a BMP file with transparency.

from aspose.imaging import Image
from aspose.imaging.fileformats.png import BmpOptions

source_path = "input.png"
output_path_def = "result_def.bmp"
output_path_def_2 = "result_def-2.bmp"
output_path_bitfields = "result_bitfields.bmp"
# Load a PNG image from a file.
with Image.load(source_path) as pngImage:
	# BMP image is saved with transparency support by default. 
	# If you want to explicitly specify such mode, the BmpOptions's `compression` property should be set to BitmapCompression.BITFIELDS.
	# The BitmapCompression.BITFIELDS compression method is the default compression method in the BmpOptions.
	# So the same result of exporting a Bmp image with transparency can be achieved by either one of the following ways.
	# With an implicit default options:
	pngImage.save(output_path_def)
	# With an explicit default options:
	pngImage.save(output_path_def_2, BmpOptions())
	# Specifying the BitmapCompression.BITFIELDS compression method:
	bmp_options = BmpOptions()
	bmp_options.compression = BitmapCompression.BITFIELDS
	pngImage.save(output_path_bitfields, bmp_options)

The example shows how to export a BMP with the RGB compression type.


from aspose.imaging import Image
from aspose.imaging.fileformats.bmp import BitmapCompression
from aspose.imaging.imageoptions import BmpOptions

source_path = "input.png"
output_path = "output.png"
# Load a PNG image from a file.
with Image.load(source_path) as pngImage:
	# BMP image is saved with transparency support by default, that is achieved by using the BitmapCompression.BITFIELDS compression method. 
	# To save a BMP image with the RGB compression method, the BmpOptions with the `compression` property set to BitmapCompression.RGB should be specified.
	bmp_options = BmpOptions()
	bmp_options.compression = BitmapCompression.RGB
	pngImage.save(output_path, bmp_options)