Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to overlay images on top of each other

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.

like image 493
IntFooBar Avatar asked Oct 30 '25 17:10

IntFooBar


1 Answers

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();
}
like image 50
IntFooBar Avatar answered Nov 01 '25 12:11

IntFooBar