Skip to content

Instantly share code, notes, and snippets.

@tuna2134
Created April 25, 2023 02:29
Show Gist options
  • Select an option

  • Save tuna2134/85e1b7c97b971c22221e91cc66ef8fef to your computer and use it in GitHub Desktop.

Select an option

Save tuna2134/85e1b7c97b971c22221e91cc66ef8fef to your computer and use it in GitHub Desktop.
use image::{DynamicImage, GenericImageView};
use ndarray::{Array, Array3, Axis};
fn image_to_ndarray(img: &DynamicImage) -> Array3<u8> {
let (width, height) = img.dimensions();
let mut data = vec![0u8; (width * height * 3) as usize];
img.to_rgb8().into_raw().iter().enumerate().for_each(|(i, pixel)| {
data[i] = *pixel;
});
Array::from_shape_vec((height as usize, width as usize, 3), data).unwrap().reversed_axes()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment