I am utilising a cargo lib called image = 0.23.14 where, I am trying to overlay the image on top of each other.
On their repository, there is an example where you can concat the images side by side.
use image::{
GenericImage,
GenericImageView,
ImageBuffer,
Pixel,
Primitive
};
fn h_concat<I, P, S>(images: &[I]) -> ImageBuffer<P, Vec<S>>
where
I: GenericImageView<Pixel = P>,
P: Pixel<Subpixel = S> + 'static,
S: Primitive + 'static {
let mut imgbuf = image::ImageBuffer::new(100, 100);
for img in images {
imgbuf.copy_from(img, 0, 0).unwrap();
}
imgbug
}
fn main() -> Result<()> {
h_concat(&[
image::open("images/img1.png").unwrap(),
image::open("images/img2.png").unwrap(),
]).save("random.png").unwrap();
Ok(())
}
I am wondering what if I want to append more files together.
Okay, after some fiddling and doing a bit more research on the documentation. I did found that there is a method image::imageops::overlay which solves my problem.
use image::{DynamicImage, imageops};
fn h_concat(mut base: DynamicImage, imgs: &[DynamicImage]) -> DynamicImage {
for img in imgs {
imageops::overlay(&mut base, img, 0, 0);
}
base
}
fn main() -> Result<()> {
let base = image::open("images/img1.png").unwrap();
h_concat(base, &[
image::open("images/img2.png").unwrap()
]).save("random.png").unwrap();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With