The fastest way to convert an image to WebP from the command line is Google's own encoder, cwebp:
cwebp -q 80 input.jpg -o output.webpThat reads input.jpg, encodes it at quality 80, and writes output.webp. The -q value runs 0 to 100 (higher is better and larger), and 80 is the sweet spot I reach for on photographs: visibly indistinguishable from the source at a fraction of the bytes. WebP typically lands 25 to 35 percent smaller than an equivalent JPEG and far smaller than a PNG, which is the whole reason to bother.
cwebp is the canonical tool because it ships from the same team that designed the format, so it exposes every knob the codec has. The rest of this page covers lossless mode, animated GIFs, batching a folder, and the newer rival worth knowing about, AVIF, which goes smaller still.
Install cwebp (and gif2webp)
Both tools live in the same webp package:
# macOS
brew install webp
# Debian / Ubuntu
sudo apt install webp
# Fedora
sudo dnf install libwebp-toolsThat gives you cwebp (still images), gif2webp (animated GIFs), and dwebp (decode WebP back to PNG). Confirm it landed:
cwebp -versionLossless WebP for graphics and screenshots
For logos, screenshots, line art, anything with flat color and hard edges, drop the quality knob and go lossless. Lossy compression smears crisp edges; lossless keeps them pixel-perfect and still beats PNG on size:
cwebp -lossless input.png -o output.webpThere is also -near_lossless, which allows tiny, invisible changes for a meaningful extra size cut on graphics. It runs 0 (most aggressive) to 100 (off):
cwebp -near_lossless 60 input.png -o output.webpRule of thumb: photographs use lossy -q 80; UI assets and anything with text in it use -lossless or -near_lossless.
Convert an animated GIF to WebP
This is where the savings get dramatic. Animated GIF is an ancient, palette-limited format; animated WebP routinely cuts the file to a fraction of the GIF. Use gif2webp, not cwebp (which only handles still frames):
gif2webp -q 80 input.gif -o output.webpIf you want the smallest possible animated file and do not mind the encoder choosing settings for you, -min_size optimizes purely for size:
gif2webp -min_size input.gif -o output.webpOne honest caveat: if the "GIF" is really a short video clip somebody exported to GIF, the better answer is to skip WebP and serve an actual MP4 or WebM, which compresses motion far more efficiently. See convert WebM to MP4 (and back) with ffmpeg for that direction, and if you are stuck with GIF, optimize and compress a GIF from the command line covers squeezing the GIF itself.
Batch a whole folder
Converting one file is rarely the real job. A shell loop handles a directory:
for f in *.jpg; do
cwebp -q 80 "$f" -o "${f%.jpg}.webp"
done${f%.jpg} strips the .jpg extension so photo.jpg becomes photo.webp. To sweep PNGs as well, run a second loop swapping *.jpg for *.png and %.jpg for %.png, or glob both. For a recursive walk through subfolders:
find . -name '*.jpg' -exec cwebp -q 80 {} -o {}.webp \;That leaves slightly awkward photo.jpg.webp names; if that bothers you, wrap the conversion in a tiny script that rebuilds the name. For a one-off batch the loop above is plenty.
Going smaller: AVIF
WebP is no longer the newest option. AVIF (AV1 Image File Format) generally compresses better than WebP at the same visual quality, especially on photographs, and as of 2026 it is supported in every major browser: Chrome since 85, Firefox since 93, Edge since 121, and Safari since 16.4 (March 2023), for roughly 94 percent global coverage. The encoder is avifenc, from the libavif project:
# macOS
brew install libavif
# Debian / Ubuntu
sudo apt install libavif-binThen convert, with a quality value that also runs 0 to 100 (100 is lossless here):
avifenc -q 75 input.jpg output.avifffmpeg can also write AVIF if you already have it installed, by encoding a single frame with the AV1 codec:
ffmpeg -i input.jpg -c:v libaom-av1 -still-picture 1 output.avifavifenc is the more direct tool and exposes AVIF-specific options (speed, chroma, lossless) cleanly, so prefer it unless ffmpeg is the only thing you have. For a refresher on ffmpeg syntax in general, the ffmpeg command cheat sheet is the reference.
WebP or AVIF? AVIF wins on file size and is now broadly safe to ship, but its encode is slower and the few percent of older clients still need a fallback. The robust answer is to serve both and let the browser pick the first format it understands:
<picture>
<source srcset="photo.avif" type="image/avif" />
<source srcset="photo.webp" type="image/webp" />
<img src="photo.jpg" alt="..." />
</picture>ImageMagick as the fallback
If you already have ImageMagick installed for other work, it converts to WebP too (it delegates to the same libwebp under the hood):
magick input.png -quality 80 output.webpTwo things to get right. First, it is magick, not convert: ImageMagick 7 renamed the command, and convert is the deprecated v6 spelling that prints a warning (or is gone entirely) on a modern install. Second, ImageMagick only writes WebP if the libwebp delegate was compiled in, so if you hit a "no encode delegate" error, your build is missing it and the dedicated cwebp is the cleaner fix anyway. ImageMagick can write AVIF as well (magick input.png output.avif), again only when its AVIF delegate is present. For a single format conversion, cwebp and avifenc are simpler and give better control; reach for magick when it is already in your toolchain.
Quality guidance
A few defaults that have served me well, so you are not guessing:
| Source | Command | Why |
|---|---|---|
| Photograph (web) | cwebp -q 80 | Indistinguishable from source, big size cut |
| Photograph (smallest viable) | avifenc -q 60 / cwebp -q 65 | AVIF holds up better at low quality |
| Logo, icon, screenshot, text | cwebp -lossless | No edge smearing |
| Graphic, can tolerate a nudge | cwebp -near_lossless 60 | Lossless look, smaller file |
| Animated GIF | gif2webp -q 80 (or -min_size) | Fraction of the GIF size |
Always eyeball the output before committing to a quality value across a batch. Open the WebP next to the source; if you cannot tell them apart, you can usually go a few points lower.
FAQ
See also
- The ffmpeg command cheat sheet: convert, crop, trim, and compress media from the CLI, the hub for the whole media-conversion set.
- Optimize and compress an animated GIF: squeeze a GIF you are stuck with using gifsicle's lossy and color options.
- Convert WebM to MP4 (and back) with ffmpeg: when a "GIF" is really a video clip, serve it as a real video instead.
Sources
Authoritative references this article was fact-checked against.
- cwebp command-line options (Google WebP official docs)developers.google.com
- gif2webp command-line options (Google WebP official docs)developers.google.com
- WebP precompiled utilities (Google official)developers.google.com
- avifenc manual (libavif official)github.com
- AVIF image format browser support (Can I Use)caniuse.com
- ImageMagick 7 command-line tools, magick as primary command (official docs)imagemagick.org





