■ DynamicImage 열거형의 get_pixel/put_pixel 메소드를 사용해 색상 반전 이미지 파일을 생성하는 방법을 보여준다.
▶ 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 |
use image::GenericImage; use image::GenericImageView; use image::Rgba; fn main() { let argument_vector : Vec<String> = std::env::args().collect(); if argument_vector.len() < 2 { println!("[USAGE] test_project imagefile"); return; } let source_file_path : String = argument_vector[1].clone(); let file_path_element_vector : Vec<&str> = source_file_path.split(".").collect(); let target_file_path : String = format!("{}_inversed.jpg", file_path_element_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 (image_width, image_height) = source_dynamic_image.dimensions(); for y in 0..image_height { for x in 0..image_width { let source_rgba : Rgba<u8> = source_dynamic_image.get_pixel(x, y); let target_rgba : Rgba<u8> = Rgba([255 - source_rgba[0], 255 - source_rgba[1], 255 - source_rgba[2], source_rgba[3]]); source_dynamic_image.put_pixel(x, y, target_rgba); } } source_dynamic_image.save(target_file_path).unwrap(); } |