Working with Images in Rust: image, photon-rs, and Other Libraries

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

Working with Images in Rust: Libraries, Tools, and Practical Examples



Rust is a modern systems programming language that is increasingly used for tasks related to graphics and image processing. Thanks to its high performance, safe memory management, and rich ecosystem of crates, Rust is well-suited for creating image processing tools: from simple format converters to complex filters and computer vision algorithms.



In this article, we will review the key libraries for working with images in Rust, explore their capabilities, and provide working code examples.



Main Libraries for Working with Images



The Rust ecosystem offers several mature crates for image processing. The most popular ones are:



  • image — a fundamental library for loading, saving, and basic image transformations. Supports PNG, JPEG, GIF, BMP, ICO, TIFF, and other formats.
  • photon-rs — a high-performance library for applying photo filters and effects. Works with Rust and WebAssembly.
  • imageproc — an extension for image, providing advanced algorithms: Hough transforms, segmentation, morphological operations.
  • rusttype — a library for working with fonts and rendering text on images.


Installation and Setup



Add the dependencies to your Cargo.toml file:



[dependencies]image = "0.25"photon-rs = "0.3"imageproc = "0.25"


Loading, Resizing, and Saving an Image



Let's start with a basic example — load an image, reduce it by half, and save it in a different format.



use image::{DynamicImage, GenericImageView, ImageFormat};use std::fs::File;

fn main() -> Result<(), Box<dyn std::error::Error>> { // Load the image let img = image::open("input.jpg")?; println!("Size: {} x {}", img.width(), img.height());

// Scale to half the original size let resized = img.resize_exact( img.width() / 2, img.height() / 2, image::imageops::FilterType::Lanczos3 );

// Save in PNG format let mut output = File::create("output.png")?; resized.write_to(&mut output, ImageFormat::Png)?;

println!("Done! Saved as output.png"); Ok(())}


Applying Photo Filters with photon-rs



The photon-rs library allows you to quickly apply filters, adjust colors, and add effects.



use photon_rs::PhotonImage;use photon_rs::transform::resize;use photon_rs::filters::filter;

fn main() { // Open the image let mut img = photon_rs::open_image("input.jpg");

// Apply the "sepia" filter filter(&mut img, "sepia", 1.0);

// Adjust brightness photon_rs::channels::brighten(&mut img, 30);

// Resize let resized = resize(&img, 800, 600, photon_rs::transform::SamplingFilter::Nearest);

// Save the result photon_rs::save_image(resized, "output_photon.jpg"); println!("Filter applied, saved to output_photon.jpg");}


Working with Pixels and Drawing



Sometimes you need to directly manipulate pixels or draw primitives. For this, we use image in conjunction with imageproc.



use image::{RgbImage, Rgb};use imageproc::drawing::draw_filled_circle_mut;

fn main() { // Create an empty 400x300 image with a white background let mut img = RgbImage::new(400, 300);

// Fill the background with light blue for pixel in img.pixels_mut() { *pixel = Rgb([200, 220, 255]); }

// Draw a red circle (x=200, y=150, radius=80) draw_filled_circle_mut( &mut img,\

Recommendations