TechEarl

How to Convert Images to WebP (and AVIF) From the Command Line

Convert JPG, PNG, and animated GIF to WebP from the command line with Google's cwebp and gif2webp, batch a whole folder, and go further with AVIF via avifenc. ImageMagick's magick as the fallback.

Ishan Karunaratne⏱️ 8 min readUpdated
Share thisCopied
Convert JPG, PNG, and animated GIF to WebP from the command line with cwebp and gif2webp, batch a folder, and use AVIF via avifenc for smaller files.

The fastest way to convert an image to WebP from the command line is Google's own encoder, cwebp:

bash
cwebp -q 80 input.jpg -o output.webp

That 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:

bash
# macOS
brew install webp

# Debian / Ubuntu
sudo apt install webp

# Fedora
sudo dnf install libwebp-tools

That gives you cwebp (still images), gif2webp (animated GIFs), and dwebp (decode WebP back to PNG). Confirm it landed:

bash
cwebp -version

Lossless 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:

bash
cwebp -lossless input.png -o output.webp

There 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):

bash
cwebp -near_lossless 60 input.png -o output.webp

Rule 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):

bash
gif2webp -q 80 input.gif -o output.webp

If you want the smallest possible animated file and do not mind the encoder choosing settings for you, -min_size optimizes purely for size:

bash
gif2webp -min_size input.gif -o output.webp

One 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:

bash
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:

bash
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:

bash
# macOS
brew install libavif

# Debian / Ubuntu
sudo apt install libavif-bin

Then convert, with a quality value that also runs 0 to 100 (100 is lossless here):

bash
avifenc -q 75 input.jpg output.avif

ffmpeg can also write AVIF if you already have it installed, by encoding a single frame with the AV1 codec:

bash
ffmpeg -i input.jpg -c:v libaom-av1 -still-picture 1 output.avif

avifenc 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:

html
<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):

bash
magick input.png -quality 80 output.webp

Two 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:

SourceCommandWhy
Photograph (web)cwebp -q 80Indistinguishable from source, big size cut
Photograph (smallest viable)avifenc -q 60 / cwebp -q 65AVIF holds up better at low quality
Logo, icon, screenshot, textcwebp -losslessNo edge smearing
Graphic, can tolerate a nudgecwebp -near_lossless 60Lossless look, smaller file
Animated GIFgif2webp -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

Sources

Authoritative references this article was fact-checked against.

Tagswebpcwebpgif2webpavifavifencimagemagickCLIimage optimization

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Tech Architect · Software Engineer · AI/DevOps

Tech architect and software engineer with 20+ years building software, Linux systems, and DevOps infrastructure, and lately working AI into the stack. Currently Chief Technology Officer at a healthcare tech startup, which is where most of these field notes come from.

Keep reading

Related posts

How to Convert a PSD to PNG From the Command Line

Convert a PSD to PNG from the command line with ImageMagick. The one trick that matters: design.psd[0] selects the flattened composite, so you get one PNG instead of a folder full of separate layers.