Skip to navigation
Create a transparent Canvas Image and put another transparent picture on top with rust
23.10.23
```rust use image::io::Reader as ImageReader; use image::RgbaImage; if _spath.exists() == false { let _r = fs::create_dir_all(_spd).await; let mut f = std::fs::File::open(&_path).expect("no file found"); let metadata = std::fs::metadata(&_path).expect("unable to read metadata"); let mut buffer = vec![0; metadata.len() as usize]; f.read(&mut buffer).expect("buffer overflow"); let format = image::guess_format(&buffer).unwrap(); let mut reader = ImageReader::with_format(std::io::Cursor::new(buffer), format); reader.no_limits(); let img = reader.decode().unwrap(); if img.width() < width || img.height() < height { let mut width_offset: i64 = 0; let mut height_offset: i64 = 0; let mut thumb_width: u32 = img.width(); let mut thumb_height: u32 = img.height(); if img.width() < width { width_offset = ((width - img.width()) / 2) as i64; } else { thumb_width = width; } if img.height() < height { height_offset = ((height - img.height()) / 2) as i64; } else { thumb_height = height; } println!("Img Width: {:?}", img.width()); println!("Img Height: {:?}", img.height()); println!("Thumb Width: {:?}", thumb_width); println!("Thumb Height: {:?}", thumb_height); println!("Width offset: {:?}", width_offset); println!("Height offset: {:?}", height_offset); let thumb = img.thumbnail(thumb_width, thumb_height); let mut canvas2 = RgbaImage::new(width, height); image::imageops::overlay(&mut canvas2, &thumb, width_offset, height_offset); canvas2.save(&_sp).unwrap(); } else { let thumb = img.thumbnail(width, height); thumb.save(&_sp).unwrap(); } } ```
https://docs.rs/open_ui/latest/open_ui/struct.RgbaImage.html
Reply
Anonymous
Information Epoch 1742052837
Live free or die.
Home
Notebook
Contact us