[−][src]Function raster::editor::resize
pub fn resize(
src: &mut Image,
w: i32,
h: i32,
mode: ResizeMode
) -> RasterResult<()>
Resize an image to a given width, height and mode.
Examples
Resize Fit
use raster::{editor, Color, Image, ResizeMode, BlendMode, PositionMode}; // Create an image from file let mut image1 = raster::open("tests/in/sample.jpg").unwrap(); let mut image2 = raster::open("tests/in/portrait.jpg").unwrap(); // Resize it editor::resize(&mut image1, 200, 200, ResizeMode::Fit).unwrap(); editor::resize(&mut image2, 200, 200, ResizeMode::Fit).unwrap(); // Superimpose images on a gray background let mut bg = Image::blank(200, 200); editor::fill(&mut bg, Color::hex("#CCCCCC").unwrap()).unwrap(); let image1 = editor::blend(&bg, &image1, BlendMode::Normal, 1.0, PositionMode::TopLeft, 0, 0).unwrap(); let image2 = editor::blend(&bg, &image2, BlendMode::Normal, 1.0, PositionMode::TopLeft, 0, 0).unwrap(); raster::save(&image1, "tests/out/test_resize_fit_1.jpg").unwrap(); raster::save(&image2, "tests/out/test_resize_fit_2.jpg").unwrap();
The gray box shows the 200x200 imaginary box that the images "fit" in.
Resize Fill
use raster::{editor, Color, Image, ResizeMode}; // Create an image from file let mut image1 = raster::open("tests/in/sample.jpg").unwrap(); let mut image2 = raster::open("tests/in/portrait.jpg").unwrap(); // Resize it editor::resize(&mut image1, 200, 200, ResizeMode::Fill).unwrap(); editor::resize(&mut image2, 200, 200, ResizeMode::Fill).unwrap(); raster::save(&image1, "tests/out/test_resize_fill_1.jpg").unwrap(); raster::save(&image2, "tests/out/test_resize_fill_2.jpg").unwrap();
The image fills up the entire 200x200 box.
Resize to Exact Width
use raster::{editor, Color, Image, ResizeMode}; // Create an image from file let mut image1 = raster::open("tests/in/sample.jpg").unwrap(); let mut image2 = raster::open("tests/in/portrait.jpg").unwrap(); // Resize it editor::resize(&mut image1, 200, 200, ResizeMode::ExactWidth).unwrap(); editor::resize(&mut image2, 200, 200, ResizeMode::ExactWidth).unwrap(); raster::save(&image1, "tests/out/test_resize_exact_width_1.jpg").unwrap(); raster::save(&image2, "tests/out/test_resize_exact_width_2.jpg").unwrap();
The images will have a width of 200. The height is auto-calculated.
Resize to Exact Height
use raster::{editor, Color, Image, ResizeMode}; // Create an image from file let mut image1 = raster::open("tests/in/sample.jpg").unwrap(); let mut image2 = raster::open("tests/in/portrait.jpg").unwrap(); // Resize it editor::resize(&mut image1, 200, 200, ResizeMode::ExactHeight).unwrap(); editor::resize(&mut image2, 200, 200, ResizeMode::ExactHeight).unwrap(); raster::save(&image1, "tests/out/test_resize_exact_height_1.jpg").unwrap(); raster::save(&image2, "tests/out/test_resize_exact_height_2.jpg").unwrap();
The images will have a height of 200. The width is auto-calculated.
Resize to Exact Dimension
use raster::{editor, Color, Image, ResizeMode}; // Create an image from file let mut image1 = raster::open("tests/in/sample.jpg").unwrap(); let mut image2 = raster::open("tests/in/portrait.jpg").unwrap(); // Resize it editor::resize(&mut image1, 200, 200, ResizeMode::Exact).unwrap(); editor::resize(&mut image2, 200, 200, ResizeMode::Exact).unwrap(); raster::save(&image1, "tests/out/test_resize_exact_1.jpg").unwrap(); raster::save(&image2, "tests/out/test_resize_exact_2.jpg").unwrap();
The images will be resized to the exact dimension ignoring aspect ratio.