Getting pixels of image as grayscale 0.0 to 1.0

I'm trying to read the pixels of an image, as 0 to 1 on a grayscale, so I wrote the below code:


use image::{GenericImageView, Pixels, DynamicImage, FilterType, ImageBuffer, Luma};

fn main() {
    let img = image::open("self.jpg").unwrap();
        let gray: ImageBuffer<Luma<u8>, Vec<u8>> = img.to_luma();

        gray.iter().map(|it| it).for_each( |it : &u8|
            println!("grayscale on 0 to 1: {}", it/255)
        );
}

The issue I've is by dividing it/255 which is &u8 / u64

So, I tried re-rotting it as: f64::from(it)/255f64) but I got the below error:

error[E0277]: the trait bound `f64: std::convert::From<&u8>` is not satisfied
  --> src/main.rs:33:45
   |
33 |         println!("grayscale on 0 to 1: {}", f64::from(it)/255f64));
   |                                             ^^^^^^^^^ the trait `std::convert::From<&u8>` is not implemented for `f64`
   |
   = help: the following implementations were found:
             <f64 as std::convert::From<f32>>
             <f64 as std::convert::From<i16>>
             <f64 as std::convert::From<i32>>
             <f64 as std::convert::From<i8>>
           and 3 others
   = note: required by `std::convert::From::from`

Then i tried fixing it as: f64::from(it.cloned())/255f64 but got this error:

error[E0599]: no method named `cloned` found for type `&u8` in the current scope
  --> src/main.rs:33:58
   |
33 |         println!("grayscale on 0 to 1: {}", f64::from(it.cloned())/255f64));
   |                                                          ^^^^^^
   |
   = note: the method `cloned` exists but the following trait bounds were not satisfied:
           `&mut &u8 : std::iter::Iterator`
           `&mut u8 : std::iter::Iterator`

So I added mutability to be as:

    let mut gray: ImageBuffer<Luma<u8>, Vec<u8>> = img.to_luma();
    gray.iter_mut().map(|it| it).for_each( |it: &mut u8|
        println!("grayscale on 0 to 1: {}", f64::from(it.cloned())/255f64));

But I still getting:

error[E0599]: no method named `cloned` found for type `&mut u8` in the current scope
  --> src/main.rs:33:58
   |
33 |         println!("grayscale on 0 to 1: {}", f64::from(it.cloned())/255f64));
   |                                                          ^^^^^^
   |
   = note: the method `cloned` exists but the following trait bounds were not satisfied:
           `&mut &mut u8 : std::iter::Iterator`
           `&mut u8 : std::iter::Iterator`

(it as f64) / 255.0

I got this error:

error[E0606]: casting `&u8` as `f64` is invalid
  --> src/main.rs:33:45
   |
33 |         println!("grayscale on 0 to 1: {}", (it as f64) / 255.0));
   |                                             ^--^^^^^^^^
   |                                             ||
   |                                             |help: dereference the expression: `*it`
   |                                             cannot cast `&u8` as `f64`

error: aborting due to previous error

The code generated this error, is:

    let gray = img.to_luma();
    gray.iter().map(|it| it).for_each( |it|
        println!("grayscale on 0 to 1: {}", (it as f64) / 255.0));

Oh, I forgot about the reference: (*it as f64) / 255_f64

(you can also instead add a .copied() adaptor right after the .iter())

1 Like

Thanks a lot, both solutions worked, I liked using the *