WebP Image Conversion and Manipulation

0.4.0 · active · verified Fri Apr 17

The `webp` library provides Python bindings for `libwebp`, enabling encoding, decoding, reading, and saving of WebP images. It integrates seamlessly with Pillow (PIL) for image manipulation, making it easy to convert between various image formats and WebP. The library is actively maintained with regular updates, typically bundling the latest `libwebp` version to ensure security and performance. Current version is 0.4.0.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a simple Pillow image, encode it into WebP format bytes, and then decode those bytes back into a Pillow image using the `webp` library's `imencode` and `imdecode` functions.

from PIL import Image
import webp
import io

# Create a dummy image using Pillow
img = Image.new('RGB', (100, 100), color = 'red')

# Encode image to WebP bytes with quality 80
webp_bytes = webp.imencode(img, quality=80)
print(f"Encoded WebP image size: {len(webp_bytes)} bytes")

# Decode WebP bytes back into a Pillow Image
decoded_img = webp.imdecode(webp_bytes)
print(f"Decoded image mode: {decoded_img.mode}, size: {decoded_img.size}")

# Optionally save to a file (replace 'output.webp' with desired path)
# webp.imsave('output.webp', decoded_img, quality=80)

# Example of reading directly from a file (if you had one)
# loaded_img = webp.imread('path/to/image.webp')

view raw JSON →