Manipulating WebP Images

Convert GIFF Image Frame to WebP Image

Aspose.Imaging enables you to extract a frame or frames from any existing GIFF image and convert it to WebP format. This article shows how you can extract a particular frame from a GIFF image and convert it to a WebP image. Following are the steps to convert a GIFF frame to a WebP image.

  1. Load an existing GIFF image into an instance of Image using the factory method load.
  2. Create and initialize an instance of GifImage class.
  3. Create and initialize an instance of WebPImage class.
  4. Access the GIFF image Frame with GifImage property.
  5. Create and initialize an instance of WebPFrameBlock class.
  6. Add WebP frame to WebP image block list.
  7. Set WebP image options.
  8. Save WebP image.

Following is the code demonstration.

import aspose.pycore as aspycore
from aspose.imaging import *
from aspose.imaging.fileformats.gif import *
from aspose.imaging.fileformats.gif.blocks import *
from aspose.imaging.fileformats.webp import *
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
file_name = os.path.join(data_dir, "template.gif")
# Load GIFF image into the instance of image class.
with Image.load(file_name) as image:
if aspycore.is_assignable(image, GifImage):
# Create an instance of GIFF image class.
gif = aspycore.as_of(image, GifImage)
# Load an existing WebP image into the instance of WebPImage class.
with WebPImage(image.width, image.height, None) as webp:
# Loop through the GIFF frames
for block in gif.blocks:
if not aspycore.is_assignable(block, GifFrameBlock):
continue
# Convert GIFF block to GIFF Frame
gif_block = aspycore.as_of(block, GifFrameBlock)
# Create an instance of WebP Frame instance by passing GIFF frame to class constructor.
new_block = WebPFrameBlock(gif_block)
new_block.top = gif_block.top
new_block.left = gif_block.left
new_block.duration = (gif_block.control_block.delay_time if gif_block.control_block is not None else 10)
# Add WebP frame to WebP image block list
webp.add_block(new_block)
# Set Properties of WebP image.
webp.options.anim_background_color = 0xf
webp.options.anim_loop_count = 0
webp.options.quality = 50
webp.options.lossless = False
# Save WebP image.
webp.save(os.path.join(data_dir, "result.webp"))
if delete_output:
os.remove(os.path.join(data_dir, "result.webp"))

Create WebP Image

WebP is a new image format that provides lossless and lossy compression for images on the web. Developers can use the WebP image format to create smaller and richer images that can help make the web faster. Using Aspose.Imaging for Python via .NET API developers can create WebP image. This article demonstrates the use of WebPOptions and Image classes to create a WebP image. The release of Aspose.Imaging 3.3.0 contains the WebPOptions class. With the help of WebPOptions class developer can create WebP image. The code snippet provided below demonstrates how to use it.

from aspose.imaging import Image
from aspose.imaging.imageoptions import WebPOptions
from aspose.imaging.sources import FileCreateSource
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
file_name = os.path.join(data_dir, "result.webp")
# Create an instance of WebPOptions class and set properties
with WebPOptions() as image_options:
image_options.lossless = True
image_options.source = FileCreateSource(file_name, False)
# Create an instance of image class by using WebOptions instance that you have just created.
with Image.create(image_options, 500, 500) as image:
image.save()
if delete_output:
os.remove(file_name)

Exporting Image to WebP

Aspose.Imaging lets you save files in Webp format. This article shows how to save a file in Webp format with Aspose.Imaging, and it also discusses some of the settings that can be used when saving to this format. WebPOptions is a specialized class in the ImageOptions namespace used to manipulate WebP images. To export to Webp, create an instance of the Image class, either loaded from an existing image file or created from scratch. This article explains how. In the example below, an existing image is loaded by passing the file path to the Image class static Load method. Once it is loaded, save the image using the Image class Save method, and supply a WebPOptions object as the second argument WebPOptions class properties can be set while conversion. Some of the properties are quality and lossless. Following is the code demonstration.

from aspose.imaging import Image
from aspose.imaging.imageoptions import WebPOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
file_name = os.path.join(data_dir, "template.bmp")
# Create an instance of image class.
with Image.load(file_name) as image:
# Create an instance of WebPOptions class and set properties
options = WebPOptions()
options.quality = 50
options.lossless = False
image.save(os.path.join(data_dir, "result.webp"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.webp"))

Exporting WebP to Other Image Formats

Using Aspose.Imaging you can convert WebP image to other image formats. This article shows how you can convert a WebP image to other image format. In the example below, an existing WebP image is loaded by passing the file path to the Image class static Load method. Once it is loaded, save the image using the Image class Save method, and supply a instance of BmpOptions as the second argument. In this way a WebP image will be converted to a BMP image.

from aspose.imaging import Image
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
file_name = os.path.join(data_dir, "template.webp")
# Load WebP image into the instance of image class.
with Image.load(file_name) as image:
# Save the image in bmp format.
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))

Extract Frames From WebP Image

Aspose.Imaging enables you to extract a frame or frames from any existing WebP image and convert it to other image formats. This article shows how you can extract a particular frame from a WebP image and convert it to a Raster image. Aspose.Imaging.FileFormats.Webp.WebPImage has a property called Blocks. Once an existing WebP image is loaded, Blocks property will contain the array of frames inside the WebP image. Using Blocks property you can access an individual frame image. In the example below, an existing WebP image is loaded by passing the file path to the Aspose.Imaging.FileFormats.Webp.WebPImage class. Once it is loaded, check the Blocks property. If it is greater then zero then it means frames inside the WebP image have been loaded. Now access a particular frame and convert it into any Raster Image.

import aspose.pycore as aspycore
from aspose.imaging import RasterImage
from aspose.imaging.fileformats.webp import WebPImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
file_name = os.path.join(data_dir, "template.webp")
# Load an existing WebP image into the instance of WebPImage class.
with WebPImage(file_name) as image:
if image.page_count > 2:
# Access a particular frame from WebP image and cast it to Raster Image
block = aspycore.as_of(image.pages[2], RasterImage)
# Save the Raster Image to a BMP image.
block.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))

Update WebP Image

There was an OutOfMemoryException when saving WebP file after resize, crop and rotate&flip actions. Aspose.Imaging for Python via .NET provides a simple solution to self-update WebP image. The following code snippet shows how to update the WebP file.

import aspose.pycore as aspycore
from aspose.imaging import Image, ResizeType, Rectangle, RotateFlipType
from aspose.imaging.fileformats.webp import WebPImage
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
input_file = os.path.join(data_dir, "template.webp")
output_file = os.path.join(data_dir, "result.webp")
with aspycore.as_of(Image.load(input_file), WebPImage) as image:
image.resize(300, 450, ResizeType.HIGH_QUALITY_RESAMPLE)
image.crop(Rectangle(10, 10, 200, 300))
image.rotate_flip_all(RotateFlipType.ROTATE_90_FLIP_X)
image.save(output_file)
if delete_output:
os.remove(output_file)

Memory Strategy Optimization

Load and create of WebP images can be proceeded using memory strategy optimization - i.e. limiting memory buffer size for operation.

from aspose.imaging import LoadOptions, Image
from aspose.imaging.imageoptions import WebPOptions
from aspose.imaging.sources import FileCreateSource
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
file_name = os.path.join(data_dir, "created.webp")
# Example 1. Setting a memory limit of 50 megabytes for operations on the created WebP image
with WebPOptions() as image_options:
image_options.source = FileCreateSource(file_name, False)
image_options.buffer_size_hint = 50
with Image.create(image_options, 1000, 1000) as image:
# Do something with the created image
image.save()
if delete_output:
os.remove(file_name)
# Example 2. Setting a memory limit of 20 megabytes for operations on the loaded WebP image
file_name = os.path.join(data_dir, "template.webp")
load_options = LoadOptions()
load_options.buffer_size_hint = 20
with Image.load(file_name, load_options) as image:
pass
# Example 3. Settings a memory limit of 30 megabytes for export-to-webp operation
load_options = LoadOptions()
load_options.buffer_size_hint = 30
with Image.load(os.path.join(data_dir, "template.png"), load_options) as image:
image.save(os.path.join(data_dir, "result.webp"), WebPOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.webp"))