All posts
Compress PNG via CLI
Lossless PNG compression with optipng and metadata stripping.
Open online converterPackages to install
Install optipng — it tries Deflate settings and shrinks PNG without changing pixels.
optipng
sudo apt-get update
sudo apt-get install -y optipngHow to run the command
optipng -o2 balances speed and size, -strip all removes EXIF and text chunks, and -out writes a new file.
optipng -o2 -strip all -out photo.min.png photo.pngDockerfile example
Build a minimal Ubuntu image with the required packages and run the conversion inside the container.
FROM ubuntu:24.04
RUN apt-get update && \
apt-get install -y --no-install-recommends \
optipng && \
rm -rf /var/lib/apt/lists/*
WORKDIR /work
docker build -t compress-png .
docker run --rm -v "$PWD:/work" -w /work compress-png \
optipng -o2 -strip all -out photo.min.png photo.pngExample commands
Run these locally after installing the packages, or inside the container from the Dockerfile.
optipng -o2 -strip all -out photo.min.png photo.pngoptipng -o3 -strip all -out photo.min.png photo.png