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: IHasXmpData, IHasMetadata, ImageOptionsBase
Constructors
Name | Description |
---|---|
BmpOptions() | Initializes a new instance of the BmpOptions class. |
BmpOptions(bmp_options) | Initializes a new instance of the BmpOptions class. |
Properties
Name | Type | Access | Description |
---|---|---|---|
bits_per_pixel | int | r/w | Gets or sets the image bits per pixel count. |
buffer_size_hint | int | r/w | Gets or sets the buffer size hint which is defined max allowed size for all internal buffers. |
compression | BitmapCompression | r/w | Gets or sets the compression type. The default compression type is BitmapCompression.BITFIELDS, that allows saving a BmpImage with transparency. |
disposed | bool | r | Gets a value indicating whether this instance is disposed. |
full_frame | bool | r/w | Gets or sets a value indicating whether [full frame]. |
keep_metadata | bool | r/w | Gets a value whether to keep original image metadata on export. |
multi_page_options | MultiPageOptions | r/w | The multipage options |
palette | IColorPalette | r/w | Gets or sets the color palette. |
resolution_settings | ResolutionSetting | r/w | Gets or sets the resolution settings. |
source | Source | r/w | Gets or sets the source to create image in. |
vector_rasterization_options | VectorRasterizationOptions | r/w | Gets or sets the vector rasterization options. |
xmp_data | XmpPacketWrapper | r/w | Gets or sets the XMP metadata container. |
Methods
Name | Description |
---|---|
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…
Example # 2: The following example creates a palettized grayscale BMP image and then saves…
Constructor: BmpOptions(bmp_options)
BmpOptions(bmp_options)
Initializes a new instance of the BmpOptions class.
Parameters:
Parameter | Type | Description |
---|---|---|
bmp_options | BmpOptions | The 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…
Example # 3: The following example creates a palettized grayscale BMP image and then saves…
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 following example creates a palettized grayscale BMP image and then saves…
Example # 3: Decompress BMP image which was previously compressed using DXT1 compression a…
Example # 4: The example shows how to export a BMP from a PNG file while keeping the alpha…
Example # 5: The example shows how to export a BMP with the RGB compression type.
Method: clone()
clone()
Clones this instance.
Returns
Type | Description |
---|---|
ImageOptionsBase | Returns 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 following example creates a palettized grayscale BMP image and then saves it to a file.
from os.path import join as path_join
from aspose.imaging import Image, ColorPaletteHelper, ResolutionSetting, Graphics, Point, Color
from aspose.imaging.sources import FileCreateSource
from aspose.imaging.imageoptions import BmpOptions
from aspose.imaging.fileformats.bmp import BitmapCompression
from aspose.imaging.brushes import LinearGradientBrush
directory = "c:\\temp\\"
createOptions = BmpOptions()
# Save to a file
createOptions.source = FileCreateSource(path_join(directory, "output.palette8bit.bmp"), False)
# Use 8 bits per pixel to reduce the size of the output image.
createOptions.bits_per_pixel = 8
# Set the standard 8-bit grayscale color palette which covers all grayscale colors.
# If the processed image contains only grayscale colors, then its palettized version
# is visually indistinguishable from a non-palletized one.
createOptions.palette = ColorPaletteHelper.create_8_bit_grayscale(False)
# Save without compression.
# You can also use RLE-8 compression to reduce the size of the output image.
createOptions.compression = BitmapCompression.RGB
# Set the horizontal and vertical resolution to 96 dpi.
createOptions.resolution_settings = ResolutionSetting(96.0, 96.0)
# Create a BMP image of 100 x 100 px and save it to a file.
with Image.create(createOptions, 100, 100) as image:
graphics = Graphics(image)
gradientBrush = LinearGradientBrush(Point(0, 0), Point(image.width, image.height), Color.black, Color.white)
# Fill the image with a grayscale gradient
graphics.fill_rectangle(gradientBrush, image.bounds)
image.save()
Decompress BMP image which was previously compressed using DXT1 compression algorithm.
from aspose.imaging import Image
from aspose.imaging.imageoptions import BmpOptions
with Image.load("CompressedTiger.bmp") as image:
image.save("DecompressedTiger.bmp", BmpOptions())
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)