Image Processing in Rust: resize and crop
Rust is a modern systems programming language increasingly used for tasks requiring high performance and reliability. Image processing is no exception. In this article, we will look at two key operations — resizing and cropping — using the Rust ecosystem. The main libraries: image (loading, saving, basic manipulations) and imageproc (advanced processing).
Why Rust for image processing?
Rust offers:
- Memory safety — no leaks or data races.
- Maximum performance — comparable to C/C++.
- Rich ecosystem — the
imagecrate supports JPEG, PNG, GIF, BMP, WEBP and other formats. - Concurrency — easily scale processing across multi-core processors.
Installing dependencies
Add to Cargo.toml:
[dependencies]image = "0.25"imageproc = "0.25" # for advanced filters (optional)Check the latest versions on crates.io.
Loading and saving an image
Let's start with a basic example — open a file and save a copy:
use image::{GenericImageView, ImageFormat};
fn main() -> Result<(), Box<dyn std::error::Error>> { // Load the image let img = image::open("input.jpg")?; // Print dimensions println!("Width: {}, Height: {}", img.width(), img.height()); // Save in a different format img.save("output.png")?; Ok(())}The image::open method automatically detects the format by file extension or content.
Resizing
The resize function takes new width and height, as well as an interpolation filter. Available filters: Nearest, Triangle, CatmullRom, Gaussian, Lanczos3 and others.
use image::{imageops::FilterType, GenericImageView};
fn resize_image(path: &str, new_width: u32, new_height: u32) -> Result<(), Box<dyn std::error::Error>> { let img = image::open(path)?; // Resize with Lanczos3 filter (high quality) let resized = img.resize(new_width, new_height, FilterType::Lanczos3); // Save the result resized.save("resized.jpg")?; println!("Size changed: {}x{} → {}x{}", img.width(), img.height(), new_width, new_height); Ok(())}Tip: For fast processing use FilterType::Nearest (no interpolation), for maximum quality — Lanczos3.
Proportional resizing
To maintain the aspect ratio, use resize_to_fill (fills the area) or resize_to_fit (fits into a rectangle):
use image::imageops::FilterType;
fn resize_proportional(path: &str, max_width: u32, max_height: u32) -> Result<(), Box<dyn std::error::Error>> { let img = image::open(path)?; // Fit into a rectangle (preserving proportions) let fitted = img.resize_to_fit(max_width, max_height, FilterType::Triangle); fitted.save("fitted.jpg")?; // Fill the area (will crop excess) let filled = img.resize_to_fill(max_width, max_height, FilterType::Triangle); filled.save("filled.jpg")?; Ok(())}Cropping
Cropping is done using the crop method, which takes the coordinates of the top-left corner (x, y) and the dimensions of the area:
use image::Gener Also in library
Swagger/OpenAPI generation for TypeScript: best tools
Dependency Injection Container for Java: Spring IoC, Google Guice, and Dagger
PDF Generation in Rust: Overview of Libraries and Tools
Working with S3 and Cloud Storage in Rust: aws-sdk-s3 Library