Crate image_ndarray

Crate image_ndarray 

Source
Expand description

Tests License Version

§image-ndarray

Zero-copy implementations for the Image crate to convert to and from ndarrays.

Working with ndarrays allows for easy manipulation of pixel values.

While there is another crate called ndarray-image, that works with dedicated types. This crate does not and implements the methods directly onto the ImageBuffer objects.

§Usage

Add the crate to your project:

cargo add image-ndarray

Then in your project, when you want to use the ndarrays on the images, make sure to add the prelude:

use image_ndarray::prelude::*;

§Example

Adding a simple value:

use image::Rgba32FImage;
use image_ndarray::prelude::*;

let mut my_image = Rgba32FImage::new(1920, 1080);

assert!(my_image.to_vec() == vec![0.0; 1920 * 1080 * 4]);

let mut array = my_image.as_mut_ndarray();
array += 1.0;

assert!(my_image.to_vec() == vec![1.0; 1920 * 1080 * 4]);

Adding another image:

use image::Rgba32FImage;
use image_ndarray::prelude::*;

let mut my_image = Rgba32FImage::new(1920, 1080);
let second_image = Rgba32FImage::from_vec(1920, 1080, vec![1.0; 1920 * 1080 * 4]).unwrap();
assert!(my_image.to_vec() == vec![0.0; 1920 * 1080 * 4]);

let mut array = my_image.as_mut_ndarray();
array += &second_image.as_ndarray();

assert!(my_image.to_vec() == vec![1.0; 1920 * 1080 * 4]);

Dividing another image (just any math operation supported by ndarray):

use image::Rgba32FImage;
use image_ndarray::prelude::*;

let mut my_image = Rgba32FImage::from_vec(1920, 1080, vec![1.0; 1920 * 1080 * 4]).unwrap();
let second_image = Rgba32FImage::from_vec(1920, 1080, vec![2.0; 1920 * 1080 * 4]).unwrap();
assert!(my_image.to_vec() == vec![1.0; 1920 * 1080 * 4]);

let mut array = my_image.as_mut_ndarray();
array /= &second_image.as_ndarray();

assert!(my_image.to_vec() == vec![0.5; 1920 * 1080 * 4]);

Convert image to array:

use image::Rgba32FImage;
use image_ndarray::prelude::*;

let my_image = Rgba32FImage::from_vec(1920, 1080, vec![1.0; 1920 * 1080 * 4]).unwrap();
let array = my_image.to_ndarray();

assert!(array.as_slice().unwrap().to_vec() == vec![1.0; 1920 * 1080 * 4]);

Or just create an image from the provided array:

use image::Rgba32FImage;
use image_ndarray::prelude::*;
use ndarray::Array3;

let array = Array3::from_elem((1080, 1920, 4), 1.0);
let my_image = Rgba32FImage::from_ndarray(array).unwrap();

assert!(my_image.to_vec() == vec![1.0; 1920 * 1080 * 4]);

Modules§

prelude
Implementations for ndarray casting and conversions for the ImageBuffer

Enums§

Error
Global error object for the image-ndarray crate.