resize an image in rust

To resize an image in Rust, we can use the image crate. Here's how you can use this crate to resize an image:

main.rs
use image::{GenericImageView, ImageBuffer, DynamicImage};

fn resize_image(image: DynamicImage, new_width: u32, new_height: u32) -> ImageBuffer<image::Rgba<u8>, Vec<u8>> {
    // Resize the image using the Lanczos3 resampling filter
    let resized_image = image.resize(new_width, new_height, image::imageops::FilterType::Lanczos3);
    // Convert the resized image to an ImageBuffer
    let resized_image_buffer = ImageBuffer::from_fn(new_width, new_height, |x, y| {
        image::Rgba(resized_image.get_pixel(x, y).data)
    });
    resized_image_buffer
}
559 chars
12 lines

In this code, we take an input image, along with the desired new width and height, and use the resize method provided by the image crate. We specify the Lanczos3 filter type for resampling, which provides high quality results. Finally, we convert the resulting DynamicImage to an ImageBuffer, which can be saved or further processed as necessary.

gistlibby LogSnag