All posts

PNG to WebP via CLI

Convert PNG to WebP with cwebp — lossy or lossless.

Open online converter

Packages to install

Install the webp package. cwebp accepts both PNG and JPEG.

webp
apt install
sudo apt-get update
sudo apt-get install -y webp

How to run the command

For the web, cwebp -q 90 is typical. Use -lossless when you need an exact pixel copy.

cwebp
cwebp -q 90 photo.png -o photo.webp

Dockerfile example

Build a minimal Ubuntu image with the required packages and run the conversion inside the container.

Dockerfile
FROM ubuntu:24.04

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        webp && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /work
docker build && docker run
docker build -t png-to-webp .
docker run --rm -v "$PWD:/work" -w /work png-to-webp \
  cwebp -q 90 photo.png -o photo.webp

Example commands

Run these locally after installing the packages, or inside the container from the Dockerfile.

Example 1
cwebp -q 90 photo.png -o photo.webp
Example 2
cwebp -lossless photo.png -o photo.webp