All posts

Compress JPEG via CLI

Lossless: jpegtran optimizes Huffman tables and strips EXIF.

Open online converter

Packages to install

libjpeg-turbo-progs provides jpegtran, a lossless JPEG optimizer.

libjpeg-turbo-progs
apt install
sudo apt-get update
sudo apt-get install -y libjpeg-turbo-progs

How to run the command

jpegtran -copy none strips metadata, -optimize rebuilds Huffman tables, and -outfile writes the result. Pixels are not re-encoded.

jpegtran
jpegtran -copy none -optimize -outfile photo.min.jpg 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 \
        libjpeg-turbo-progs && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /work
docker build && docker run
docker build -t compress-jpg .
docker run --rm -v "$PWD:/work" -w /work compress-jpg \
  jpegtran -copy none -optimize -outfile photo.min.jpg photo.jpg

Example commands

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

Example 1
jpegtran -copy none -optimize -outfile photo.min.jpg photo.jpg
Example 2
jpegtran -copy none -optimize -progressive -outfile photo.min.jpg photo.jpg