Create empty image with the image crate of Rust

image empty

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:


name = "create-empty-image"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at

image = "0.24"

Then we add the code:


use image::{RgbImage, ImageBuffer};

fn main() {

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

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

and run

cargo run