Image Formats • • By Umar

PNG to WebP Batch Command Line: Whole Folder Guide

A whole folder is not one file repeated. Pick the mode first, keep the outputs in their own tree, and verify the batch before the originals go anywhere.

PNG to WebP Batch Command Line: Whole Folder Guide

Converting one PNG to WebP is a single command. Converting a folder is a different job: which encoding mode, where the outputs go, what happens to subfolders, what happens when two files share a name, and what happens when the command runs a second time. Most batch guides stop at a one-line loop. The loop is the easy part. This guide covers the parts that break a real folder, with scripts for cwebp, ImageMagick, Pillow, and sharp, on macOS, Linux, and Windows. The scripts assume still PNG files — cwebp does not accept animated PNGs, which need img2webp instead. Keep the PNG originals until the checks at the end pass.

Decide the mode before you write the loop

Every script here needs the same choice first, and changing it later means re-encoding the whole folder. Lossless keeps every pixel exactly and suits logos, icons, screenshots, and flat colour. Lossy at quality 80 suits photographs, where a little discarded detail is hard to see. Near-lossless, around 60, sits between the two for screenshots and interface graphics. Our PNG to WebP quality guide works through the choice image by image.

One setting catches people out. Quality 100 is not lossless: cwebp -q 100 still uses lossy encoding, at the highest quality. Lossless needs the -lossless flag, and in lossless mode -q controls how much effort the encoder spends shrinking the file, not how much detail it removes. Google's published comparisons put lossless WebP about 26% smaller than the equivalent PNG, and lossy WebP 25–34% smaller than JPEG at the same visual quality. Lossy WebP also converts colour to YUV 4:2:0, sampling colour at half resolution — usually invisible in a photograph, sometimes visible as bleed around red text on white. Fringes around screenshot text mean the mode is wrong, not the quality number.

Install cwebp and check it works

cwebp is the reference encoder, part of Google's libwebp. Install it from the system package manager: sudo apt install webp on Debian and Ubuntu, sudo dnf install libwebp-tools on Fedora, sudo pacman -S libwebp on Arch, brew install webp on macOS, winget install Google.libwebp on Windows. Google also publishes precompiled archives, currently version 1.6.0. Then run cwebp -version; a command not found answer usually means reopening the terminal so it picks up the new path.

The one-folder loop

The quotes around "$f" matter: without them, a filename with a space splits into two arguments and the command fails, or encodes the wrong file. Write the outputs to a separate folder, so the sources stay clean and the batch is easy to delete and redo:

mkdir -p webp
for f in *.png; do cwebp -q 80 "$f" -o "webp/${f%.png}.webp"; done

On Windows, PowerShell does the same job without WSL, and ChangeExtension swaps the extension cleanly on names with several dots:

Get-ChildItem -Filter *.png -File | ForEach-Object {
  $out = Join-Path webp ([System.IO.Path]::ChangeExtension($_.Name, 'webp'))
  & cwebp -q 80 $_.FullName -o $out
}

One pattern to avoid: find . -exec cwebp {} -o {}.webp \; writes photo.png.webp, because {} is the whole filename including its extension. Strip the extension first, as the scripts here do.

Subfolders, without flattening the tree

A flat loop only sees the current folder. For a tree, find walks every subfolder, and the output path must mirror the input path or files from different subfolders land on top of each other:

find . -path ./webp -prune -o -type f -iname '*.png' -print0 |
while IFS= read -r -d '' f; do
  out="webp/${f%.png}.webp"
  mkdir -p "$(dirname "$out")"
  [ -f "$out" ] && continue
  cwebp -q 80 "$f" -o "$out"
done

The -print0 and read -d '' pair is the part most guides leave out: filenames are separated by a zero byte, so names with spaces survive, where for img in $(find ...) splits on whitespace and breaks. The -prune exclusion keeps find from scanning the output tree it writes into. For speed, the same search through xargs -0 -P 4 -I{} runs four conversions at once — encoding is CPU-bound, so more workers than cores mostly add contention — and the sequential loop stays right for filenames with quote characters.

Two files, one name: the collision problem

A folder can hold photo.jpg and photo.png side by side, and both convert to photo.webp. Run the PNG batch and then the JPEG batch into the same output folder, and the second overwrites the first without a warning. Case collides too: Photo.PNG and photo.png are different files on Linux and the same file on the default macOS and Windows filesystems. Fix it before the first run: one input format per output folder, the original extension kept in the name (photo.png.webp), or a pre-flight list of planned outputs that stops on duplicates. A collision found afterwards means re-encoding. The same trap waits in Pillow, where with_suffix(".webp") on two stems produces the same path.

Reruns: skip what is done, redo what changed

A batch of a thousand files can easily be interrupted. Skipping existing outputs handles the simple case, but a source edited after its output keeps the stale output, and a half-written output from a killed process looks complete to the skip test. Compare timestamps — skip only when the output is newer than its source, [ "$out" -nt "$f" ] && continue — and encode to a temporary name, moved into place only on success:

cwebp -q 80 "$f" -o "$out.tmp" && mv "$out.tmp" "$out"

The move is instant on the same filesystem, so the output is either fully there or not there at all. That single property is what makes a rerun safe to trust.

What cwebp quietly drops: metadata and hidden pixels

By default, cwebp copies no metadata: -metadata defaults to none, and ICC profiles, EXIF, and XMP are dropped unless requested — -metadata icc keeps the profile, -metadata all keeps everything. A wide-gamut image converted without its profile can look duller in a colour-managed viewer, which then assumes sRGB. In lossless mode, cwebp may change RGB values hidden under fully transparent pixels to compress better; -exact preserves them for files that will be edited or converted back to PNG. Resize while converting: -resize 1600 0 sets the width and calculates the height, and -resize_mode down_only leaves smaller images alone. -preset must come first, because it overwrites the other parameters except -q — placed after -m, it silently undoes it, and a later -q or -m invalidates the -z shortcut the same way.

ImageMagick, Pillow, and sharp: the same job in other tools

ImageMagick needs no loop: mogrify is its built-in batch command. Check WebP support with magick -list format | grep -i webp, then write into a separate folder — mogrify without -path or -format overwrites the originals in place, with no undo:

magick mogrify -path webp -format webp -quality 80 *.png

Lossless goes through a define, -define webp:lossless=true, as do finer knobs such as -define webp:method=6 -define webp:alpha-quality=100. ImageMagick calls libwebp underneath but decodes into its own representation first, so expect it to run slower than cwebp. One behaviour differs: at quality 100, ImageMagick switches to lossless, while cwebp at -q 100 stays lossy.

Pillow fits a batch inside a Python script: walk the tree with rglob, mirror it, skip existing outputs, and use lossless=True instead of quality=80, method=6 for lossless mode. ImageOps.exif_transpose applies stored orientation before saving, which matters when the script also handles JPEGs; Pillow keeps RGBA transparency without extra settings, and saves only the first frame of an animated GIF. sharp, in Node.js, suits a build step and needs Node.js 20.9 or newer: sharp(p).rotate().webp({ quality: 80, effort: 4 }).toFile(out), with concurrency bounded to a small pool rather than an unbounded Promise.all. Effort runs 0 to 6 and defaults to 4; in a DEV.to measurement of one 1920×1076 photo at quality 80 (sharp 0.35.4, median of five runs on an Apple M1 Pro), effort 2 wrote 110.4 KB in 60 ms against effort 6's 102.3 KB in 214 ms — most of the saving for a fraction of the time. One photo on one machine: read the gap, not the exact figures, as the lesson. The Squoosh command-line tool, which older guides recommend, is deprecated by Google and no longer maintained; sharp is the replacement. FFmpeg can convert through its libwebp encoder, but its controls map less directly onto cwebp's, so it earns its place only inside an existing FFmpeg pipeline.

When WebP comes out bigger, or refuses

A WebP larger than its PNG is not an encoder bug. Google's WebP FAQ lists three causes: a few-colour PNG converted lossy gains colours from the YUV conversion — use lossless for flat graphics; a lossy source saved as PNG keeps every artefact exactly under lossless encoding — use lossy WebP instead; or a quality target above the source's effective quality. Two hard limits also shape a batch: WebP stores 8 bits per channel, so a 16-bit PNG loses precision — keep the PNG masters for anything edited again — and the maximum dimension is 16,383 by 16,383 pixels. Our guide to oversized PNGs covers the size side of the same problem, and the WebP vs PNG vs JPG comparison puts measured sizes side by side.

Verify the batch before you delete anything

Count first: WebP files should match PNG inputs, minus deliberate skips. Then inspect a sample: webpinfo -summary shows a chunk stats summary, and decoding one file with dwebp confirms something readable was written. Open a transparent output over dark and light backgrounds — right on white and wrong on black usually means the alpha channel was discarded.

A short checklist for the next batch

  1. Pick the mode first. Lossless for logos and text, near-lossless 60 for screenshots, quality 80 lossy for photographs — and -q 100 is not lossless.
  2. Write to a separate folder mirroring the source tree, outside the source tree or excluded from the search.
  3. Check for name collisions before encoding: same stem, different extension, or different case.
  4. Make reruns safe. Skip outputs newer than their sources; encode to a temporary name, moved into place on success.
  5. Verify before deleting. Counts, a decoded sample, and a transparency check on dark and light backgrounds.

A batch script is worth keeping. Save the one that fits your folder, with the mode and output folder written in it, and the next folder of PNGs takes one command.

Umar

Founder, Format Studio

Umar builds Format Studio's free creator tools and writes the practical guides.

Frequently Asked Questions

How do I batch convert a folder of PNGs to WebP?

Use a loop that writes to a separate output folder: for f in *.png; do cwebp -q 80 "$f" -o "webp/${f%.png}.webp"; done. Quote the filename variable so names with spaces work, and create the output folder first. On Windows, the PowerShell version in this guide does the same job without WSL.

How do I convert PNGs in subfolders too?

Walk the tree with find and mirror the folder structure in the output folder, creating each output folder as needed. Use find -print0 with a read loop so filenames with spaces survive. Exclude the output folder from the search when it sits inside the source tree, or put it outside the source tree entirely.

Does WebP keep transparency?

Yes. WebP keeps the alpha channel, and cwebp compresses alpha losslessly by default (alpha_q defaults to 100). Transparency is lost only if you ask for it with -noalpha, or flatten it by blending onto a background with -blend_alpha. Check a transparent output over both dark and light backgrounds before trusting the batch.

Why is cwebp -q 100 not lossless?

The -q value sets lossy quality, and 100 is the highest lossy quality — it still discards detail. Lossless encoding needs the -lossless flag. In lossless mode, -q changes meaning and controls how much effort the encoder spends shrinking the file, not how much detail it removes.

Why is my WebP bigger than the PNG?

Three causes cover most cases. A few-colour PNG converted lossy gains colours from the YUV conversion — use lossless for flat graphics. A lossy source saved as PNG keeps its artefacts exactly under lossless encoding — use lossy WebP instead. Or the quality target is higher than the source's effective quality, spending bytes on detail the source never had.

Does cwebp work on animated PNGs?

No. cwebp does not accept animated PNG or WebP files as input. A sequence of PNG frames goes through img2webp, and an animated GIF through gif2webp. Still PNGs are the only input this guide's cwebp scripts handle.

Convert a Folder Without Uploading It

Drop a single PNG in, pick WebP, and download the result — all in your browser, nothing uploaded. Use the scripts above for a whole folder.

Open Image Converter →