Last updated: February 5, 2025

<span aria-hidden="true" id="optimizing-image-processing-in-rust-with-parallelism-and-rayon"></span>

# Optimizing image processing in Rust with parallelism and Rayon

![Tim Koschützki](/assets/images/teammates/avatar-tim-kos-1.jpg?dpl=dpl_3fBRD5jmFtSDABLXGJJyXU1nVXf8)

**Tim Koschützki**

Co-founder · Berlin, Germany · Show bio

[](https://x.com/tim%5Fkos)[](https://github.com/tim-kos)

Processing images, such as converting formats and resizing dimensions, are common tasks in many applications. Rust, with its performance and safety guarantees, is an excellent choice for building efficient image processing tools. In this guide, we will explore how to use Rust and open-source libraries to convert and resize images.

<span aria-hidden="true" id="introduction-to-image-processing-in-rust"></span>

## Introduction to image processing in Rust

Rust’s powerful ecosystem and performance make it ideal for handling image processing tasks. By leveraging open-source libraries, you can build image processing applications that are both efficient and reliable.

<span aria-hidden="true" id="setting-up-your-rust-environment"></span>

## Setting up your Rust environment

Install Rust using [rustup⁠](https://rustup.rs/):

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

```

Verify the installation:

```bash
rustup --version
cargo --version

```

Create a new project:

```bash
cargo new image_processor
cd image_processor

```

<span aria-hidden="true" id="utilizing-open-source-libraries-for-image-processing"></span>

## Utilizing open-source libraries for image processing

We will use the `image` crate, a popular open-source library in Rust for image processing tasks. Add the following dependencies to your `Cargo.toml`:

```toml
[dependencies]
image = "0.24.8"
anyhow = "1.0.79"
rayon = "1.8.0"
thiserror = "1.0.56"
log = "0.4.20"
env_logger = "0.10.1"

```

<span aria-hidden="true" id="converting-images-step-by-step-guide"></span>

## Converting images: step-by-step guide

To convert images between formats (for example, from PNG to JPEG), use the `image` crate as follows:

```rust
use anyhow::{Context, Result};
use image::ImageFormat;
use std::path::Path;

fn convert_image(input_path: &Path, output_path: &Path, output_format: ImageFormat) -> Result<()> {
    let img = image::open(input_path)
        .with_context(|| format!("Failed to open image: {}", input_path.display()))?;
    img.save_with_format(output_path, output_format)
        .with_context(|| format!("Failed to save image: {}", output_path.display()))?;
    Ok(())
}

```

Usage:

```rust
use std::path::Path;
use anyhow::Result;
use image::ImageFormat;

fn main() -> Result<()> {
    let input_path = Path::new("input.png");
    let output_path = Path::new("output.jpg");
    convert_image(input_path, output_path, ImageFormat::Jpeg)?;
    Ok(())
}

```

<span aria-hidden="true" id="resizing-images-practical-examples"></span>

## Resizing images: practical examples

Resizing images can be done efficiently using the `image` crate:

```rust
use image::imageops::FilterType;
use anyhow::Result;
use std::path::Path;

fn resize_image(input_path: &Path, output_path: &Path, new_width: u32, new_height: u32) -> Result<()> {
    let img = image::open(input_path)?;
    let resized = img.resize(new_width, new_height, FilterType::Lanczos3);
    resized.save(output_path)?;
    Ok(())
}

```

Usage:

```rust
use std::path::Path;
use anyhow::Result;

fn main() -> Result<()> {
    let input_path = Path::new("input.jpg");
    let output_path = Path::new("resized.jpg");
    resize_image(input_path, output_path, 800, 600)?;
    Ok(())
}

```

<span aria-hidden="true" id="performance-guidelines"></span>

## Performance guidelines

To achieve optimal performance when processing images:

* Use `FilterType::Lanczos3` for highest quality resizing.
* Use `FilterType::Triangle` for faster, lower quality resizing.
* Enable parallel processing only for batch operations with ten or more images.
* Keep image dimensions under 8000x8000 pixels for optimal memory usage.

<span aria-hidden="true" id="parallel-processing-with-rayon"></span>

## Parallel processing with Rayon

For batch processing multiple images efficiently, leverage Rayon to parallelize the work:

```rust
use rayon::prelude::*;
use anyhow::Result;
use std::path::Path;

fn process_directory(input_dir: &Path, output_dir: &Path) -> Result<()> {
    std::fs::create_dir_all(output_dir)?;

    let entries: Vec<_> = std::fs::read_dir(input_dir)?
        .filter_map(Result::ok)
        .filter(|entry| {
            entry.path().extension()
                .map(|ext| ext.eq_ignore_ascii_case("jpg") || ext.eq_ignore_ascii_case("png"))
                .unwrap_or(false)
        })
        .collect();

    entries.par_iter()
        .try_for_each(|entry| {
            let input_path = entry.path();
            let output_path = output_dir.join(entry.file_name());
            resize_image(&input_path, &output_path, 800, 600)
        })?;

    Ok(())
}

```

Usage example for batch processing:

```rust
use anyhow::Result;
use std::path::Path;

fn main() -> Result<()> {
    env_logger::init();

    let input_dir = Path::new("input_images");
    let output_dir = Path::new("processed_images");

    process_directory(input_dir, output_dir)?;
    Ok(())
}

```

<span aria-hidden="true" id="benefits-of-using-rust-for-image-processing"></span>

## Benefits of using Rust for image processing

Rust provides several advantages for image processing tasks:

* Performance: Rust offers performance comparable to C/C++, making it ideal for computationally intensive tasks.
* Safety: Rust’s ownership model prevents common bugs such as null pointer dereferencing and data races.
* Concurrency: Rust simplifies writing concurrent code, enabling efficient multi-threaded processing.
* Ecosystem: The availability of crates like `image` streamlines image processing tasks.

For scalable and cloud-based image processing solutions, consider using Transloadit's[Image Processing API](/docs/robots/image-resize.md).

\#rust#image-processing#parallel-computing#open-source#image-processing-service

### 👩‍💻 Join 20k+ developers

Sign up for our [monthly newsletter](/newsletters.md) to receive direct links to 3 exclusive tech — and 2 product updates. No less, no more.

Your email:

Get access

## File uploading and encoding. Made simple.

Transloadit streamlines file handling for developers, trusted by brands like Coursera and The New York Times. We’re known for a reliable API, top-notch support, and a strong commitment to open source, with projects like [Uppy⁠](https://uppy.io) and [Tus⁠](https://tus.io) setting standards in file processing.

[Sign up](/c/)[Book a Demo](https://survey.typeform.com/to/kRg47Xi5)

No credit card needed · 5 GB included in the free plan

Cancel anytime
