How can I draw a pic from a vec

I'm trying the below:

  1. Read image from file: Done correctly
  2. Convert this image into a Vec<Vec<f64>> where f64 is the grayscale of each pixel: Done correctly, and got the vector named as pic_pix
  3. Redrawing the original pic, from the generated vector pic_pix: Nee your help, did not know where to go :frowning:
    I read this but could not manage it.

Below is my code:

use image::{GenericImageView, FilterType, Rgb, Rgba, ImageBuffer, RgbImage};
use imageproc::drawing::draw_hollow_rect_mut;
use imageproc::rect::Rect;

fn main() {
    println!("Hello, world!");

    let img = image::open("self.jpg").unwrap();

    let mut gray = img.grayscale();
    let black = Rgba([0u8, 0u8, 0u8, 0u8]);

    draw_hollow_rect_mut(&mut gray, Rect::at(60, 10).of_size(20, 20), black);

    let (r, c) = img.dimensions();   // rows, columns  // width, height

    let mut pic_gray : Vec<f64> = Vec::new();
    let mut pic_pix: Vec<Vec<f64>> = Vec::new();

    for w in 0..r {
        let mut pic_row : Vec<f64> = Vec::new();
        for h in 0..c {
            let p = img.get_pixel(w,h).data;
            let gray_scale: f64 = format!("{:.3}", (0.299 * f64::from(p[0]) +
                0.587 * f64::from(p[1]) +
                0.114 * f64::from(p[2])).ceil()/255_f64).parse().unwrap();
            println!("pixle of ({},{}) is: {}", w, h, gray_scale);
            pic_gray.push(gray_scale);
            pic_row.push(gray_scale)
        }
        pic_pix.push(pic_row);
    }

    for w in 0..r {
        println!("{:?}", pic_pix[w as usize]);
    }

/*
    // Tried the below, but did not know what to doexactly
    let mut imgbuf = image::ImageBuffer::new(r, c);
    for w in 0..r {
        for h in 0..c {
            let mut noisy = imgbuf.   .brighten(-25);
            let pixel = imgbuf.get_pixel_mut(x, y);
            let image::Rgb(data) = *pixel;
            *pixel = image::Rgb([data[0], i as u8, data[2]]);
            imgbuf.put_pixel(r, c, pic_pix[r as usize][c as usize])
        }
    }
    */
}

it's unclear to me what you mean with "draw" here, do you want it to draw to a window on the screen or save the output back to an image file ?

I want to rebuild the image file

At the end of the image readme crate you have an example of how to save an image from a raw buffer &[u8].

Generally, you will find by searching in the crate API that it is easy to build an image from a continuous buffer slice or vec but not so much from a 2-dimensional data (vec of vec). So I'd recommend switching representation.

In my case, I was doing a lot of converting from the image crate and matrices (from nalgebra). I made a small module just for that. You might find inspiration there: interop.rs