{"id":10350,"library":"webp","title":"WebP Image Conversion and Manipulation","description":"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.","status":"active","version":"0.4.0","language":"en","source_language":"en","source_url":"https://github.com/anibali/pywebp","tags":["image-processing","webp","compression","Pillow","image-conversion"],"install":[{"cmd":"pip install webp","lang":"bash","label":"Install webp"}],"dependencies":[],"imports":[{"symbol":"imencode","correct":"from webp import imencode"},{"symbol":"imdecode","correct":"from webp import imdecode"},{"symbol":"imread","correct":"from webp import imread"},{"symbol":"imsave","correct":"from webp import imsave"}],"quickstart":{"code":"from PIL import Image\nimport webp\nimport io\n\n# Create a dummy image using Pillow\nimg = Image.new('RGB', (100, 100), color = 'red')\n\n# Encode image to WebP bytes with quality 80\nwebp_bytes = webp.imencode(img, quality=80)\nprint(f\"Encoded WebP image size: {len(webp_bytes)} bytes\")\n\n# Decode WebP bytes back into a Pillow Image\ndecoded_img = webp.imdecode(webp_bytes)\nprint(f\"Decoded image mode: {decoded_img.mode}, size: {decoded_img.size}\")\n\n# Optionally save to a file (replace 'output.webp' with desired path)\n# webp.imsave('output.webp', decoded_img, quality=80)\n\n# Example of reading directly from a file (if you had one)\n# loaded_img = webp.imread('path/to/image.webp')","lang":"python","description":"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."},"warnings":[{"fix":"Ensure `passes` is set to a value like `2` or higher when using `target_size`: `webp.imencode(img, target_size=100_000, passes=2)`.","message":"When compressing images using the `target_size` option, you must set `passes` to a value greater than 1 (e.g., `passes=2`). If `passes` is not specified or set to 1, `target_size` will be ignored, resulting in a potentially larger output file than intended.","severity":"gotcha","affected_versions":"0.4.0 onwards"},{"fix":"Upgrade to `webp>=0.3.0` immediately to patch the `libwebp` vulnerability: `pip install --upgrade webp`.","message":"Versions of `webp` prior to 0.3.0 bundled `libwebp 1.0.3`, which has a known vulnerability (CVE-2023-4863) related to heap buffer overflow. While the Python API remains compatible, not upgrading poses a significant security risk.","severity":"breaking","affected_versions":"<0.3.0"},{"fix":"Always use the public API functions directly from the `webp` package (e.g., `from webp import imencode, imdecode, imread, imsave`).","message":"In version 0.4.0, the internal `_webp` module was nested within the `webp` package. Any direct imports like `from webp._webp import ...` will fail, as this module is not part of the public API and its path changed.","severity":"gotcha","affected_versions":">=0.4.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Use the public API functions directly from the `webp` package, e.g., `from webp import imencode`.","cause":"Attempting to import from the internal `_webp` module directly, whose path changed in version 0.4.0 and is not intended for direct use.","error":"ModuleNotFoundError: No module named 'webp._webp'"},{"fix":"Verify the input image is a valid Pillow `Image.Image` object and ensure its mode (e.g., 'RGB', 'RGBA') is suitable for the WebP parameters provided. Check logs for more specific `libwebp` errors if available.","cause":"The input image data is invalid, corrupted, or incompatible with WebP encoding parameters (e.g., incorrect Pillow image mode for the requested encoding type).","error":"webp.WebPError: failed to encode image"},{"fix":"Install a C compiler and `libwebp` development headers. On Debian/Ubuntu: `sudo apt-get update && sudo apt-get install build-essential libwebp-dev`. On Fedora: `sudo dnf install @development-tools libwebp-devel`. On macOS: `xcode-select --install` and `brew install webp`.","cause":"During `pip install webp`, if a pre-built binary wheel is not available for your system (OS, architecture, Python version), the package attempts to compile `libwebp` from source. This requires a C compiler (like `gcc`) and `libwebp` development headers to be installed on your system.","error":"error: command 'gcc' failed with exit status 1"}]}