How to set the pixels of an image to black in Rust

Viewed 665

I want to open an image and set all of its pixels to black. I have this code that compiles but produces an empty transparent png. I was expecting a black image.

extern crate image;                                                                   
                                                                                      
use image::{GenericImageView, ImageBuffer, RgbaImage, imageops, Rgba};
                                                                                                  
fn main() {
    let img = image::open("images/image1.png").unwrap();
                                                                                                                                                                    
    let (width, height) = img.dimensions();
    let mut out: RgbaImage = ImageBuffer::new(width, height);

    for pixel in out.enumerate_pixels_mut() {
        *pixel.2 = image::Rgba([255,255,255,1]);
    }

     out.save("out.png").unwrap();
}
1 Answers

[255,255,255,1] is a white image.

You need to use the following [0,0,0,255]. In rust the transparency is not encoded as a float unlike in js runtime with the browser for instance.

Related