Converting images to WebP using coding agents
January 18, 2026
When i am ship my blog pages, i try and convert my pngs/jpegs to WebP as it can cut transfer size drastically. I now let coding agents batch-convert PNG/JPG assets and update references automatically. I just give a prompt like this to cursor/codex: (you can change it accordingly)(just for context)
Convert all PNG images used in page.mdx to WebP. Save them under
public/images/
update the MDX references to .webp, and remove the old PNGs after verifying the new images loadInternally it runs this command :
for f in public/images/**/*.png; do cwebp -q 80 "$f" -o "${f%.png}.webp"; doneMeaning (piece by piece):
for f in public/images/**/*.png; do ... doneloops through all PNG files underpublic/images/including subfolders.**/is a recursive glob (works inzsh, or inbash)cwebp -q 80 "$f" -o "${f%.png}.webp"converts each PNG to WebP:-q 80sets quality to 80 (good balance of size vs quality)"$f"is the input file-o ...is the output path${f%.png}.webpkeeps the same filename and swaps.png→.webp- This does not delete the original PNG; it creates a new
.webpnext to it.
cwebp does the real work. It re-encodes PNG data into WebP, which is what usually reduces file size.
If you use lossy WebP (-q 80 like above), you may see a tiny quality drop.
If you want no quality loss, then u can tell that to the agent it will do it for you, but internally it does something like this:
cwebp -lossless input.png -o output.webpLossless keeps quality intact, but file sizes may be larger than lossy.
Why it helps
- Smaller payloads → faster LCP (you can test in PageSpeed Inisghts) and better mobile performance.
- Keeps quality high with ~70–85 WebP quality.