1 stable release
Uses new Rust 2024
| 1.0.1 | Jan 27, 2026 |
|---|
#187 in Images
18KB
354 lines
Resizer
A CLI tool to compress images to fit under a target size in KB by adjusting quality and downscaling if needed.
Installation
Clone the repository and build with Cargo:
git clone <repository-url>
cd resizer
cargo build --release
The binary will be available at target/release/resizer.
Usage
resizer [OPTIONS] <input> <output>
Options
--target-kb <TARGET_KB>: Target size in KB (upper bound)--format <FORMAT>: Output format: jpeg, webp, or png [default: webp]--max-width <MAX_WIDTH>: Optional max width--max-height <MAX_HEIGHT>: Optional max height--min-quality <MIN_QUALITY>: Min quality (1..=100) [default: 30]--max-quality <MAX_QUALITY>: Max quality (1..=100) [default: 95]--max-downscale-rounds <MAX_DOWNSCALE_ROUNDS>: How many downscale rounds to attempt [default: 10]--png-compression-level <PNG_COMPRESSION_LEVEL>: PNG compression level (0-9, higher = slower but smaller) [default: 6]--help: Print help
Examples
Compress an image to under 100 KB as WebP:
resizer input.jpg output.webp --target-kb 100
Compress with max dimensions and PNG format:
resizer input.png output.png --target-kb 50 --format png --max-width 800 --max-height 600 --png-compression-level 9
Behavior
- For JPEG and WebP: Uses binary search on quality to find the highest quality that fits the target size. If min quality still exceeds the target, downscales the image by 10% and retries. Pre-downscaling is applied for very large images.
- For PNG: No quality search; encodes at the specified compression level. If too large, downscales by 10% and retries.
- JPEG converts to RGB (transparent pixels become black).
- WebP preserves alpha if present.
- PNG preserves alpha if present.
License
MIT License - see LICENSE file.
Dependencies
~15MB
~270K SLoC