Create empty image with the image crate of Rust

image empty RgbImage ImageBuffer save

In this example we create an empty image using the image crate of Rust.

This is the result:

Yes, this is just a black square.

cargo new create-empty-image
cd create-empty-image
cargo add image

Results in the following file:

examples/create-empty-image/Cargo.toml

[package]
name = "create-empty-image"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
image = "0.24"

Then we add the code:

examples/create-empty-image/src/main.rs

use image::{RgbImage, ImageBuffer};

fn main() {
    empty();
}

fn empty() {
    let width = 200;
    let height = 200;

    let img: RgbImage = ImageBuffer::new(width, height);
    img.save("empty.png").unwrap();
}




and run

cargo run

Related Pages

Image - a crate to generate and manipulate images in Rust
Create empty image with white background and black borders using Rust
Create and image and draw a line in Rust

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo