WebPImage

Inheritance: java.lang.Object, com.aspose.imaging.DisposableObject, com.aspose.imaging.DataStreamSupporter, com.aspose.imaging.Image, com.aspose.imaging.RasterImage, com.aspose.imaging.RasterCachedImage, com.aspose.imaging.RasterCachedMultipageImage

All Implemented Interfaces: com.aspose.imaging.IMultipageImageExt

public final class WebPImage extends RasterCachedMultipageImage implements IMultipageImageExt

Manipulate WebP raster images with our API, using its modern features for both lossless and lossy compression, ensuring optimal image quality with reduced file sizes. Seamlessly handle extended file formats, animations, and alpha channels, while easily updating dimensions, resizing proportionally, cropping, rotating, applying filters, adjusting image parameters, and converting to other image formats for versatile web image optimization.

Constructors

ConstructorDescription
WebPImage(InputStream stream)Initializes a new instance of the WebPImage class from stream.
WebPImage(InputStream stream, LoadOptions loadOptions)Initializes a new instance of the WebPImage class from stream.
WebPImage(String path)Initializes a new instance of the WebPImage class from file.
WebPImage(String path, LoadOptions loadOptions)Initializes a new instance of the WebPImage class from file.
WebPImage(RasterImage rasterImage)Initializes a new instance of the WebPImage class from rasterImage.
WebPImage(RasterImage rasterImage, LoadOptions loadOptions)Initializes a new instance of the WebPImage class from rasterImage.
WebPImage(int width, int height, WebPOptions options)Initializes a new instance of the WebPImage class with empty image.
WebPImage(int width, int height, WebPOptions options, LoadOptions loadOptions)Initializes a new instance of the WebPImage class with empty image.

Methods

MethodDescription
getOptions()Gets the options.
getPages()
getPageCount()Gets the page count.
getFileFormat()Gets a value of file format
hasAlpha()Gets the Has alpha channel.
getPageExportingAction()Gets the page exporting action.
setPageExportingAction(PageExportingAction value)Sets the page exporting action.
addPage(RasterImage page)Adds page to the image.
addBlock(IFrame block)Adds a new Webp block.
clearBlocks()Clears all the Webp blocks.
insertBlock(int index, IFrame block)Adds a new Webp block.
removeBlock(IFrame block)Removes the Webp block.
rotate(float angle, boolean resizeProportionally, Color backgroundColor)!:RasterCachedMultipageImage.Rotate image around the center.
resize(int newWidth, int newHeight, int resizeType)Resizes the image.
resizeWidthProportionally(int newWidth, int resizeType)Resizes the width proportionally.
resizeHeightProportionally(int newHeight, int resizeType)Resizes the width proportionally.
rotateFlip(int rotateFlipType)Rotates, flips, or rotates and flips the Active frame only.
dither(int ditheringMethod, int bitsCount, IColorPalette customPalette)Performs dithering on the current image.
crop(Rectangle rectangle)Cropping the image.
crop(int leftShift, int rightShift, int topShift, int bottomShift)Crop image with shifts.
binarizeFixed(byte threshold)Binarization of an image with predefined threshold
binarizeOtsu()Binarization of an image with Otsu thresholding
binarizeBradley(double brightnessDifference, int windowSize)Binarization of an image using Bradley’s adaptive thresholding algorithm using the integral image thresholding
grayscale()Transformation of an image to its grayscale representation
adjustGamma(float gamma)Gamma-correction of an image.
adjustGamma(float gammaRed, float gammaGreen, float gammaBlue)Gamma-correction of an image.
adjustBrightness(int brightness)Adjust of a brightness for image.
adjustContrast(float contrast)Image contrasting
filter(Rectangle rectangle, FilterOptionsBase options)Filters the specified rectangle.
resize(int newWidth, int newHeight, ImageResizeSettings settings)Resizes the image.

Example: This example shows how to load a WebP image from a file and save it to PNG.

String dir = "c:\\temp\\";

// Load a WebP image from a file.
com.aspose.imaging.fileformats.webp.WebPImage webPImage = new com.aspose.imaging.fileformats.webp.WebPImage(dir + "test.webp");
try {
    // Save to PNG
    // Note that only the active frame will be stored to PNG, since PNG is not a multi-page format.
    webPImage.save(dir + "test.output.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    webPImage.dispose();
}

WebPImage(InputStream stream)

public WebPImage(InputStream stream)

Initializes a new instance of the WebPImage class from stream.

Parameters:

ParameterTypeDescription
streamjava.io.InputStreamThe stream WebP image.

WebPImage(InputStream stream, LoadOptions loadOptions)

public WebPImage(InputStream stream, LoadOptions loadOptions)

Initializes a new instance of the WebPImage class from stream.

Parameters:

ParameterTypeDescription
streamjava.io.InputStreamThe stream WebP image.
loadOptionsLoadOptionsThe load options.

WebPImage(String path)

public WebPImage(String path)

Initializes a new instance of the WebPImage class from file.

Parameters:

ParameterTypeDescription
pathjava.lang.StringThe path to file WebP Image

WebPImage(String path, LoadOptions loadOptions)

public WebPImage(String path, LoadOptions loadOptions)

Initializes a new instance of the WebPImage class from file.

Parameters:

ParameterTypeDescription
pathjava.lang.StringThe path to file WebP Image
loadOptionsLoadOptionsThe load options.

WebPImage(RasterImage rasterImage)

public WebPImage(RasterImage rasterImage)

Initializes a new instance of the WebPImage class from rasterImage.

Parameters:

ParameterTypeDescription
rasterImageRasterImageThe raster image.

WebPImage(RasterImage rasterImage, LoadOptions loadOptions)

public WebPImage(RasterImage rasterImage, LoadOptions loadOptions)

Initializes a new instance of the WebPImage class from rasterImage.

Parameters:

ParameterTypeDescription
rasterImageRasterImageThe raster image.
loadOptionsLoadOptionsThe load options.

WebPImage(int width, int height, WebPOptions options)

public WebPImage(int width, int height, WebPOptions options)

Initializes a new instance of the WebPImage class with empty image.

Parameters:

ParameterTypeDescription
widthintThe image width
heightintThe image height.
optionsWebPOptionsThe options.

WebPImage(int width, int height, WebPOptions options, LoadOptions loadOptions)

public WebPImage(int width, int height, WebPOptions options, LoadOptions loadOptions)

Initializes a new instance of the WebPImage class with empty image.

Parameters:

ParameterTypeDescription
widthintThe image width
heightintThe image height.
optionsWebPOptionsThe options.
loadOptionsLoadOptionsThe load options.

getOptions()

public WebPOptions getOptions()

Gets the options.

Value: The options.

Returns: WebPOptions - the options.

getPages()

public Image[] getPages()

Gets the blocks.

Gets the Webp blocks.

Value: The blocks.

Returns: com.aspose.imaging.Image[]

getPageCount()

public int getPageCount()

Gets the page count.

Value: The page count.

Returns: int - the page count.

getFileFormat()

public long getFileFormat()

Gets a value of file format

Returns: long - a value of file format

hasAlpha()

public boolean hasAlpha()

Gets the Has alpha channel.

Value: The Has alpha channel.

Returns: boolean - the Has alpha channel.

Example: The following example loads a WEBP image and prints information about raw data format and alpha channel.

String dir = "c:\\temp\\";
String fileName = dir + "sample.webp";
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(fileName);
try {
    com.aspose.imaging.fileformats.webp.WebPImage webpImage = (com.aspose.imaging.fileformats.webp.WebPImage) image;

    // If the active TIFF frame has alpha channel, then the entire TIFF image is considered to have alpha channel.
    System.out.printf("ImageFile=%s, FileFormat=%s, HasAlpha=%s\r\n", fileName, webpImage.getRawDataFormat(), webpImage.hasAlpha());

    int i = 0;
    for (com.aspose.imaging.fileformats.webp.IFrame frame : webpImage.getBlocks()) {
        if (frame instanceof com.aspose.imaging.fileformats.webp.WebPFrameBlock) {
            com.aspose.imaging.fileformats.webp.WebPFrameBlock frameBlock = (com.aspose.imaging.fileformats.webp.WebPFrameBlock) frame;
            System.out.printf("Frame=%s, FileFormat=%s, HasAlpha=%s\r\n", i++, frameBlock.getRawDataFormat(), frameBlock.hasAlpha());
        }
    }
} finally {
    image.dispose();
}

// The output may look like this:
// ImageFile=c:\temp\sample.webp, FileFormat=RgbIndexed1Bpp, used channels: 1, HasAlpha=False
// Frame=0, FileFormat=RgbIndexed1Bpp, used channels: 1, HasAlpha=False

getPageExportingAction()

public PageExportingAction getPageExportingAction()

Gets the page exporting action. Please note that setting this method will automatically release page resources after it is executed. It will be executed just before each page is saved.

Value: The page exporting action.

Returns: PageExportingAction - the page exporting action.

setPageExportingAction(PageExportingAction value)

public void setPageExportingAction(PageExportingAction value)

Sets the page exporting action. Please note that setting this method will automatically release page resources after it is executed. It will be executed just before each page is saved.

Value: The page exporting action.

Parameters:

ParameterTypeDescription
valuePageExportingActionthe page exporting action.

addPage(RasterImage page)

public void addPage(RasterImage page)

Adds page to the image.

Parameters:

ParameterTypeDescription
pageRasterImageThe page to add.

addBlock(IFrame block)

public void addBlock(IFrame block)

Adds a new Webp block.

Parameters:

ParameterTypeDescription
blockIFrameThe Webp block to add.

Example: This example shows how to create a multi-frame animated WebP image with the specified options.

String dir = "c:\\temp\\";

com.aspose.imaging.imageoptions.WebPOptions createOptions = new com.aspose.imaging.imageoptions.WebPOptions();
createOptions.setLossless(true);
createOptions.setQuality(100f);
createOptions.setAnimBackgroundColor((long) com.aspose.imaging.Color.getGray().toArgb());

// The default frame plus 36 + 36 additional frames.
createOptions.setAnimLoopCount(36 + 36 + 1);

// Create a WebP image of 100x100 px.
com.aspose.imaging.fileformats.webp.WebPImage webPImage = new com.aspose.imaging.fileformats.webp.WebPImage(100, 100, createOptions);
try {
    // The first circle is red
    com.aspose.imaging.brushes.SolidBrush brush1 = new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed());

    // The second circle is black
    com.aspose.imaging.brushes.SolidBrush brush2 = new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getBlack());

    // Gradually increase the angle of the red arc shape.
    for (int angle = 10; angle <= 360; angle += 10) {
        com.aspose.imaging.fileformats.webp.WebPFrameBlock block = new com.aspose.imaging.fileformats.webp.WebPFrameBlock(100, 100);
        com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(block);
        graphics.fillPie(brush1, block.getBounds(), 0, angle);

        webPImage.addBlock(block);
    }

    // Gradually increase the angle of the black arc and wipe out the red arc.
    for (int angle = 10; angle <= 360; angle += 10) {
        com.aspose.imaging.fileformats.webp.WebPFrameBlock block = new com.aspose.imaging.fileformats.webp.WebPFrameBlock(100, 100);

        com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(block);
        graphics.fillPie(brush2, block.getBounds(), 0, angle);
        graphics.fillPie(brush1, block.getBounds(), angle, 360 - angle);

        webPImage.addBlock(block);
    }

    // Save to a WebP file
    webPImage.save(dir + "output.webp");
} finally {
    webPImage.dispose();
}

clearBlocks()

public void clearBlocks()

Clears all the Webp blocks.

insertBlock(int index, IFrame block)

public void insertBlock(int index, IFrame block)

Adds a new Webp block.

Parameters:

ParameterTypeDescription
indexintThe zero-based element, at which block will be inserted.
blockIFrameThe Webp block to add.

removeBlock(IFrame block)

public void removeBlock(IFrame block)

Removes the Webp block.

Parameters:

ParameterTypeDescription
blockIFrameThe block to remove.

Note: do not forget to Dispose the block if you will not add it to some other WebPImage. |

rotate(float angle, boolean resizeProportionally, Color backgroundColor)

public void rotate(float angle, boolean resizeProportionally, Color backgroundColor)

!:RasterCachedMultipageImage.Rotate image around the center.

Parameters:

ParameterTypeDescription
anglefloatThe rotation angle in degrees. Positive values will rotate clockwise.
resizeProportionallybooleanif set to true you will have your image size changed according to rotated rectangle (corner points) projections in other case that leaves dimensions untouched and only `` image contents are rotated.
backgroundColorColorColor of the background.

resize(int newWidth, int newHeight, int resizeType)

public void resize(int newWidth, int newHeight, int resizeType)

Resizes the image.

Parameters:

ParameterTypeDescription
newWidthintThe new width.
newHeightintThe new height.
resizeTypeintThe resize type.

Example: This example loads a WEBP image and resizes it using various resizing methods.

String dir = "c:\\temp\\";

com.aspose.imaging.fileformats.webp.WebPImage image = (com.aspose.imaging.fileformats.webp.WebPImage) com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    // Scale up by 2 times using Nearest Neighbour resampling.
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);

    // Save to PNG with default options.
    image.save(dir + "upsample.nearestneighbour.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

image = (com.aspose.imaging.fileformats.webp.WebPImage) com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    // Scale down by 2 times using Nearest Neighbour resampling.
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);

    // Save to PNG with default options.
    image.save(dir + "downsample.nearestneighbour.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

image = (com.aspose.imaging.fileformats.webp.WebPImage) com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    // Scale up by 2 times using Bilinear resampling.
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample);

    // Save to PNG with default options.
    image.save(dir + "upsample.bilinear.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

image = (com.aspose.imaging.fileformats.webp.WebPImage) com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    // Scale down by 2 times using Bilinear resampling.
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample);

    // Save to PNG with default options.
    image.save(dir + "downsample.bilinear.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

resizeWidthProportionally(int newWidth, int resizeType)

public void resizeWidthProportionally(int newWidth, int resizeType)

Resizes the width proportionally.

Parameters:

ParameterTypeDescription
newWidthintThe new width.
resizeTypeintType of the resize.

resizeHeightProportionally(int newHeight, int resizeType)

public void resizeHeightProportionally(int newHeight, int resizeType)

Resizes the width proportionally.

Parameters:

ParameterTypeDescription
newHeightintThe new height.
resizeTypeintType of the resize.

rotateFlip(int rotateFlipType)

public void rotateFlip(int rotateFlipType)

Rotates, flips, or rotates and flips the Active frame only.

Parameters:

ParameterTypeDescription
rotateFlipTypeintThe rotation flip type.

dither(int ditheringMethod, int bitsCount, IColorPalette customPalette)

public void dither(int ditheringMethod, int bitsCount, IColorPalette customPalette)

Performs dithering on the current image.

Parameters:

ParameterTypeDescription
ditheringMethodintThe dithering method.
bitsCountintThe final bits count for dithering.
customPaletteIColorPaletteThe custom palette for dithering.

crop(Rectangle rectangle)

public void crop(Rectangle rectangle)

Cropping the image.

Parameters:

ParameterTypeDescription
rectangleRectangleThe rectangle.

crop(int leftShift, int rightShift, int topShift, int bottomShift)

public void crop(int leftShift, int rightShift, int topShift, int bottomShift)

Crop image with shifts.

Parameters:

ParameterTypeDescription
leftShiftintThe left shift.
rightShiftintThe right shift.
topShiftintThe top shift.
bottomShiftintThe bottom shift.

binarizeFixed(byte threshold)

public void binarizeFixed(byte threshold)

Binarization of an image with predefined threshold

Parameters:

ParameterTypeDescription
thresholdbyteThreshold value. If corresponding gray value of a pixel is greater than threshold, a value of 255 will be assigned to it, 0 otherwise.

binarizeOtsu()

public void binarizeOtsu()

Binarization of an image with Otsu thresholding

binarizeBradley(double brightnessDifference, int windowSize)

public void binarizeBradley(double brightnessDifference, int windowSize)

Binarization of an image using Bradley’s adaptive thresholding algorithm using the integral image thresholding

Parameters:

ParameterTypeDescription
brightnessDifferencedoubleThe brightness difference between pixel and the average of an s x s window of pixels centered around this pixel.
windowSizeintThe size of s x s window of pixels centered around this pixel

grayscale()

public void grayscale()

Transformation of an image to its grayscale representation

adjustGamma(float gamma)

public void adjustGamma(float gamma)

Gamma-correction of an image.

Parameters:

ParameterTypeDescription
gammafloatGamma for red, green and blue channels coefficient

adjustGamma(float gammaRed, float gammaGreen, float gammaBlue)

public void adjustGamma(float gammaRed, float gammaGreen, float gammaBlue)

Gamma-correction of an image.

Parameters:

ParameterTypeDescription
gammaRedfloatGamma for red channel coefficient
gammaGreenfloatGamma for green channel coefficient
gammaBluefloatGamma for blue channel coefficient

adjustBrightness(int brightness)

public void adjustBrightness(int brightness)

Adjust of a brightness for image.

Parameters:

ParameterTypeDescription
brightnessintBrightness value.

adjustContrast(float contrast)

public void adjustContrast(float contrast)

Image contrasting

Parameters:

ParameterTypeDescription
contrastfloatContrast value (in range [-100; 100])

filter(Rectangle rectangle, FilterOptionsBase options)

public void filter(Rectangle rectangle, FilterOptionsBase options)

Filters the specified rectangle.

Parameters:

ParameterTypeDescription
rectangleRectangleThe rectangle.
optionsFilterOptionsBaseThe options.

Example: The following example applies various types of filters to a WEBP image.

String dir = "c:\\temp\\";

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    com.aspose.imaging.fileformats.webp.WebPImage webpImage = (com.aspose.imaging.fileformats.webp.WebPImage) image;

    // Apply a median filter with a rectangle size of 5 to the entire image.
    webpImage.filter(webpImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.MedianFilterOptions(5));
    webpImage.save(dir + "sample.MedianFilter.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    com.aspose.imaging.fileformats.webp.WebPImage webpImage = (com.aspose.imaging.fileformats.webp.WebPImage) image;

    // Apply a bilateral smoothing filter with a kernel size of 5 to the entire image.
    webpImage.filter(webpImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.BilateralSmoothingFilterOptions(5));
    webpImage.save(dir + "sample.BilateralSmoothingFilter.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    com.aspose.imaging.fileformats.webp.WebPImage webpImage = (com.aspose.imaging.fileformats.webp.WebPImage) image;

    // Apply a Gaussian blur filter with a radius of 5 and a sigma value of 4.0 to the entire image.
    webpImage.filter(webpImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.GaussianBlurFilterOptions(5, 4.0));
    webpImage.save(dir + "sample.GaussianBlurFilter.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    com.aspose.imaging.fileformats.webp.WebPImage webpImage = (com.aspose.imaging.fileformats.webp.WebPImage) image;

    // Apply a Gauss-Wiener filter with a radius of 5 and a smooth value of 4.0 to the entire image.
    webpImage.filter(webpImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.GaussWienerFilterOptions(5, 4.0));
    webpImage.save(dir + "sample.GaussWienerFilter.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    com.aspose.imaging.fileformats.webp.WebPImage webpImage = (com.aspose.imaging.fileformats.webp.WebPImage) image;

    // Apply a motion wiener filter with a length of 5, a smooth value of 4.0 and an angle of 90.0 degrees to the entire image.
    webpImage.filter(webpImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.MotionWienerFilterOptions(10, 1.0, 90.0));
    webpImage.save(dir + "sample.MotionWienerFilter.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

image = com.aspose.imaging.Image.load(dir + "sample.webp");
try {
    com.aspose.imaging.fileformats.webp.WebPImage webpImage = (com.aspose.imaging.fileformats.webp.WebPImage) image;

    // Apply a sharpen filter with a kernel size of 5 and a sigma value of 4.0 to the entire image.
    webpImage.filter(webpImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.SharpenFilterOptions(5, 4.0));
    webpImage.save(dir + "sample.SharpenFilter.png", new com.aspose.imaging.imageoptions.PngOptions());
} finally {
    image.dispose();
}

resize(int newWidth, int newHeight, ImageResizeSettings settings)

public void resize(int newWidth, int newHeight, ImageResizeSettings settings)

Resizes the image.

Parameters:

ParameterTypeDescription
newWidthintThe new width.
newHeightintThe new height.
settingsImageResizeSettingsThe resize settings.