Image Compressor for Rust
Free online image compressor with Rust code examples
Working with image compressor in Rust? Our free online image compressor helps Rust developers format, validate, and process data instantly. Below you will find Rust code examples using image so you can achieve the same result programmatically in your own projects.
Try the Image Compressor Online
Use our free Image Compressor directly in your browser — no setup required.
Open Image CompressorRust Code Example
use image::{io::Reader as ImageReader, ImageFormat};
use image::imageops::FilterType;
use std::fs;
fn main() {
let img = ImageReader::open("input.jpg").unwrap().decode().unwrap();
// Resize maintaining aspect ratio
let resized = img.resize(1920, 1080, FilterType::Lanczos3);
// Save as JPEG with quality (using image crate)
resized.save("output.jpg").unwrap();
let original = fs::metadata("input.jpg").unwrap().len();
let compressed = fs::metadata("output.jpg").unwrap().len();
println!("Compressed: {} -> {} bytes ({:.1}%)",
original, compressed, compressed as f64 / original as f64 * 100.0);
}Quick Setup
Library: image
cargo add imageRust Tips & Best Practices
- The image crate supports JPEG, PNG, WebP, GIF, and more
- Use resize for exact dimensions, resize_to_fill for cover
- For JPEG quality control, use the jpeg-encoder crate directly