Skip to content

Files

Latest commit

3b40fdd · May 30, 2025

History

History
This branch is up to date with x4nth055/pythoncode-tutorials:master.

compress-image

README.md

Compress Image

Advanced Image Compressor with Batch Processing

This script provides advanced image compression and resizing features using Python and Pillow.

Features

  • Batch processing of multiple images or directories
  • Lossy and lossless compression (PNG/WebP)
  • Optional JPEG conversion
  • Resize by ratio or explicit dimensions
  • Preserve or strip metadata (EXIF)
  • Custom output directory
  • Progress bar using tqdm
  • Detailed logging

Requirements

Install dependencies:

pip install pillow tqdm

Usage

python compress_image.py [options] <input> [<input> ...]

Options

  • -o, --output-dir: Output directory (default: same as input)
  • -q, --quality: Compression quality (0-100, default: 85)
  • -r, --resize-ratio: Resize ratio (0-1, default: 1.0)
  • -w, --width: Output width (requires --height)
  • -hh, --height: Output height (requires --width)
  • -j, --to-jpg: Convert output to JPEG
  • -m, --no-metadata: Strip metadata (default: preserve)
  • -l, --lossless: Use lossless compression (PNG/WEBP)

Examples

python compress_image.py image.jpg -r 0.5 -q 80 -j
python compress_image.py images/ -o output/ -m
python compress_image.py image.png -l

License

MIT License.