Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
webp-rs
A Rust library to encode and decode WebP images via statically-linked libwebp.
⬇️ Installation
This library can be installed using Cargo. To do that, run the following command in your project's root directory:
The crate links as webp, so you import it with use webp; regardless of the package name.
[!NOTE] The first build downloads the prebuilt static binaries for your platform, so an internet connection is required (see Troubleshooting for offline builds).
🤖 Usage
Here are some examples of how to encode and decode WebP images using this library. These snippets don't have any error handling for the sake of simplicity, but you should always check for errors in production code.
Encoding
let img = open.unwrap; // an image to be encoded
let bytes = encode.unwrap; // encode the image with default settings
write.unwrap; // save the WebP to a file
Encoding with custom settings
use WebpEncoder;
let img = open.unwrap;
let mut bytes = Vecnew;
img.write_with_encoder.unwrap;
Lossless encoding
use WebpEncoder;
let img = open.unwrap;
let mut bytes = Vecnew;
img.write_with_encoder.unwrap;
Decoding
let bytes = read.unwrap; // read the WebP file
let img = decode.unwrap; // decode it into a DynamicImage
img.save.unwrap; // save it in another format
Probing (header only)
Read the image dimensions and color type without decoding the pixels — useful for validation or thumbnailing pipelines:
let bytes = read.unwrap;
let info = probe.unwrap;
println!;
The public API also exposes [encode_buffer] (encode a typed ImageBuffer directly), [WebpEncoder] / [WebpDecoder] for image-trait integration, [EncoderConfig] / [DecoderConfig] for full control, and [libwebp_version].
Encoder settings
| Setting | Range / type | Default | Meaning |
|---|---|---|---|
with_quality |
0–100 |
75 |
Visual-quality target (higher = better, larger). Ignored when lossless. |
with_quality_alpha |
0–100 |
100 |
Quality of the alpha channel. |
with_compression |
0–6 |
4 |
Compression effort (higher = slower encode, smaller file). |
with_lossless |
bool |
false |
Lossless encoding (reconstructs pixels exactly). |
with_threads |
bool |
false |
Enable libwebp's multi-threaded encoding. |
WebP is an 8-bit format: inputs are encoded as 8-bit RGB/RGBA (grayscale is expanded automatically) and decoded back to the same. 16-bit inputs are rejected as unsupported.
Runnable examples
The examples/ directory has standalone programs covering each part of the API, runnable out of the box against the bundled assets:
💣 Troubleshooting
My build fails because it can't download the binaries
The first build fetches the prebuilt static libraries for your platform over the network. For offline or air-gapped builds, download the archive for your target from binaries-webp, extract it, and point the build at it with the WEBP_BINARIES_DIR environment variable:
$ WEBP_BINARIES_DIR=/path/to/extracted/libs cargo build
📝 License
webp-rs is released under the Apache 2.0 License. See LICENSE for details.
👨🏾💻 Author
Vinicius Egidio (vinicius.io)