Sub topic 8
WebP: a modern all-round format
This chapter assumes you can already tell JPEG and PNG apart, know the difference between lossy and lossless compression, and can export a file from a design tool. It covers what WebP does, when it is a good choice, and what to watch for.
What WebP is
WebP is an image format developed by Google. One format covers the jobs that usually need several older ones:
- Lossy compression, like JPEG, for photographs.
- Lossless compression, like PNG, for sharp edges and flat color.
- Transparency (an alpha channel) in both modes.
- Animation, as a replacement for GIF.
At the same visual quality, a lossy WebP is typically 25 to 35 percent smaller than a JPEG. A lossless WebP is typically about 25 percent smaller than a PNG. Treat these as rough figures. Your results depend on the image, so always compare real files.
Lossy and lossless modes
A WebP file is either lossy or lossless. You choose the mode at export time.
| Image type | WebP mode | Replaces |
|---|---|---|
| Photograph | Lossy | JPEG |
| Screenshot, UI, flat illustration | Lossless | PNG |
| Photo with a transparent background | Lossy with alpha | PNG (much larger) |
| Short looping clip | Animated | GIF |
Lossy WebP with transparency is a useful feature. A JPEG cannot hold transparency, so a cut-out photo has traditionally needed a heavy PNG.
Exporting WebP
Many design tools can export WebP directly. Where yours cannot, use a command-line tool. This example uses cwebp, which writes a lossy file at quality 80 from a PNG.
cwebp -q 80 hero-1600.png -o hero-1600.webpFor a lossless file, add the -lossless flag:
cwebp -lossless ui-screenshot.png -o ui-screenshot.webpStart at quality 75 to 80 for photographs. Then compare the result with the original at 100 percent zoom and check the file size.
Browser support and fallbacks
All current major browsers support WebP. Older browsers may not, so some projects still provide a fallback. The picture element lets the browser pick the first format it understands and fall back to JPEG.
<picture>
<source type="image/webp" srcset="hero.webp">
<img src="hero.jpg" width="800" height="450" alt="Team working at a shared desk">
</picture>If your audience uses only current browsers, a single WebP in a normal img element is often enough. Confirm this with the developers on your project.
Common errors
- Exporting lossless for a photo. The file can end up larger than the JPEG. Use lossy for photographs.
- Recompressing a JPEG. Converting an already compressed JPEG to lossy WebP adds a second round of loss. Export from the original source when you can.
- Skipping the visual check. Low quality settings can smear fine texture and soften edges around text. Zoom in before you hand files over.
- Assuming every tool opens it. Some email clients and older editors cannot open WebP files. Check where the image will be used.
Exercise
You have a PNG screenshot of an app interface with sharp text and flat color. Which WebP mode should you use, and why?
Show answer
Use lossless WebP. Lossy compression can blur text and edges, and flat color compresses well without loss. The result is usually smaller than the PNG with no visible change.
Exercise
A teammate converts a JPEG photo to lossy WebP and the quality looks worse than the JPEG. What is the likely cause, and what should they do?
Show answer
The JPEG was already compressed, so converting it applied a second round of loss. They should export the WebP from the original uncompressed source, or raise the quality setting.