image for Rust: complete guide with examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

The image library for Rust: complete guide

\n\n

The image library is the main and most popular library for working with images in the Rust ecosystem. It provides a powerful, safe, and high-performance toolkit for loading, processing, analyzing, and saving raster images. Unlike many C/C++ counterparts, image is written in pure Rust, which guarantees memory safety and the absence of leaks characteristic of low-level libraries.

\n\n

Why is this library needed? In the modern world, image processing is required everywhere: from web services (generating thumbnails, applying watermarks) to desktop applications (photo editors, viewers) and even embedded systems (computer vision on Raspberry Pi). image allows solving these tasks without binding to external C libraries (like libpng or libjpeg), which simplifies project build and makes it cross-platform "out of the box".

\n\n

The library supports dozens of formats (PNG, JPEG, GIF, BMP, TIFF, WebP, ICO and others), provides access to individual pixels, can resize, crop, rotate, invert colors, and apply filters. It is based on the GenericImageView trait, which allows working uniformly with any image type. Thanks to Rust's design, you get maximum performance without compromising safety.

\n\n

Installation

\n

To add the image library to a Rust project, you need to add it to the Cargo.toml file:

\n\n
[dependencies]\nimage = "0.25"
\n\n

After that, run the cargo build command. Cargo will automatically download the library and all its dependencies. Working with specific formats (e.g., WebP or AVIF) may require additional feature flags, but for basic usage, the standard configuration is sufficient.

\n\n

Quick start

\n

A minimal working example — load an image, resize it, and save it:

\n\n
use image::{GenericImageView, imageops};\n\nfn main() -> Result<(), image::ImageError> {\n    // 1. Load the image from a file\n    let img = image::open("input.jpg")?;\n    \n    // 2. Resize to 300x200 pixels\n    let resized = imageops::resize(&img, 300, 200, imageops::FilterType::Lanczos3);\n    \n    // 3. Save the result as PNG\n    resized.save("output.png")?;\n    \n    println!("Image successfully processed!");\n    Ok(())\n}
\n\n

This code opens the file input.jpg, resizes it to 300x200 pixels using the Lanczos3 filter (one of the highest quality), and saves the result as output.png. Note the error handling via Result — this is the idiomatic approach in Rust.

\n\n

Main methods and functions

\n\n

1. image::open

\n

Signature: pub fn open<P: AsRef<Path>>(path: P) -> ImageResult<DynamicImage>

\n

Description: Loads an image from a file, automatically detecting the format by extension or content. Returns DynamicImage — an enum that can contain an image of any supported type (Rgb8, Rgba8, Gray8, etc.).

\n

Example:

\n
match image::open("photo.png") {\n    Ok(img) => println!("Image loaded, size: {}x{}", img.width(), img.height()),\n    Err(e) => eprintln!("Load error: {}", e),\n}
\n\n

2. DynamicImage::to_rgb8 and to_rgba8

\n

Signature: pub fn to_rgb8(&self) -> ImageBuffer<Rgb<u8>, Vec<u8>>

\n

Description: Converts a dynamic image to a specific pixel type. to_rgb8

Recommendations