All posts

Crop image via CLI

ffmpeg: crop a still image to width×height at x,y.

Open online converter

Packages to install

Only ffmpeg is required. JPEG, PNG, WebP, and GIF all work with the same crop filter.

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

How to run the command

Use -vf crop=width:height:x:y. For stills, add -frames:v 1 -update 1 so ffmpeg writes a single image. JPEG quality is -q:v 2.

ffmpeg
ffmpeg -y -i photo.jpg -vf crop=640:640:80:40 -frames:v 1 -update 1 -q:v 2 photo.crop.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 crop-image .
docker run --rm -v "$PWD:/work" -w /work crop-image \
  ffmpeg -y -i photo.jpg -vf crop=640:640:80:40 -frames:v 1 -update 1 -q:v 2 photo.crop.jpg

Example commands

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

Example 1
ffmpeg -y -i photo.jpg -vf crop=640:640:80:40 -frames:v 1 -update 1 -q:v 2 photo.crop.jpg
Example 2
ffmpeg -y -i photo.png -vf crop=1080:1920:0:0 -frames:v 1 -update 1 photo.crop.png