■ 썸네일 이미지 파일을 생성하는 방법을 보여준다.
▶ Cargo.toml
1 2 3 4 5 6 7 8 9 10 11 |
[package] name = "test_project" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] image = "0.23.14" |
▶ src/main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
use image::{self, imageops, GenericImageView}; fn main() { let target_image_size : u32 = 128; let argument_vector : Vec<String> = std::env::args().collect(); if argument_vector.len() < 2 { println!("[USAGE] image_thumb imagefile"); return; } let source_file_path : String = String::from(&argument_vector[1]); let source_file_path_vector : Vec<&str> = source_file_path.split(".").collect(); let target_file_path : String = format!("{}_thumbnail.png", source_file_path_vector[0]); println!("Source File Path : {}", source_file_path); println!("Target File Path : {}", target_file_path); let mut source_dynamic_image : image::DynamicImage = image::open(source_file_path).expect("파일 오픈시 에러가 발생했습니다."); let source_image_size : (u32, u32) = source_dynamic_image.dimensions(); let target_image_width : u32 = if source_image_size.0 > source_image_size.1 { source_image_size.1 } else { source_image_size.0 }; let mut cropped_image_buffer : image::ImageBuffer<image::Rgba<u8>, Vec<u8>> = imageops::crop ( &mut source_dynamic_image, (source_image_size.0 - target_image_width) / 2, (source_image_size.1 - target_image_width) / 2, target_image_width, target_image_width ).to_image(); let target_image_buffer : image::ImageBuffer<image::Rgba<u8>, Vec<u8>> = imageops::resize ( &mut cropped_image_buffer, target_image_size, target_image_size, imageops::Lanczos3 ); target_image_buffer.save(target_file_path).unwrap(); } |