All posts

PNG to JPEG via CLI

Convert PNG to JPEG with ffmpeg and a quality setting.

Open online converter

Packages to install

Install ffmpeg — one command re-encodes PNG to JPEG.

ffmpeg
apt install
sudo apt-get update
sudo apt-get install -y ffmpeg

How to run the command

Pass the PNG to -i and set JPEG quality with -q:v: lower is better (2 is near-lossless, 5–8 is smaller).

ffmpeg
ffmpeg -y -i photo.png -q:v 2 photo.jpg

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 \
        ffmpeg && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /work
docker build && docker run
docker build -t png-to-jpeg .
docker run --rm -v "$PWD:/work" -w /work png-to-jpeg \
  ffmpeg -y -i photo.png -q:v 2 photo.jpg

Example commands

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

Example 1
ffmpeg -y -i photo.png -q:v 2 photo.jpg
Example 2
ffmpeg -y -i photo.png -q:v 5 photo.jpeg