I need to take an image and get a list of RGB byte values. I am using the image crate. This is what I have:
extern crate image;
fn main() {
let im = image::open("wall.jpg").unwrap().to_rgb();
let data: Vec<[u8; 3]> = im.pixels().flat_map(|p| vec![p.data]).collect();
let rgb: Vec<&u8> = data.iter().flat_map(|p| p.iter()).collect();
println!("First Pixel: {} {} {}", rgb[0], rgb[1], rgb[2]);
}
This seems pretty ugly. I have to introduce an intermediate variable and I get a vector of pointers to the values I really need, so before I can do something else, I would have to map over it again to get the actual values.
All I want is a vector of u8
. How do I get that?
Well, if you just want the raw data itself, you can just call
DynamicImage::raw_pixels
:If all you're actually interested in is the first pixel though, I'd recommend calling
GenericImage::get_pixel
:Which you can then turn into a
[u8; 3]
array of RGB data: