0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ブラーをかけるンギョ on rust

Last updated at Posted at 2020-11-14
main.rs
mod application;

use self::application::Application;

fn main() {
    let mut app = Application::new();
    app.run();
}
application.rs
use image::io::Reader as ImageReader;
use image::DynamicImage;

pub struct Application {
    source: String,
    destination: String,
    img: Option<DynamicImage>,
}

impl Application {
    pub fn new() -> Application {
        Application {
            source: String::new(),
            destination: String::new(),
            img: None, 
        }
    }

    pub fn run(&mut self) {
        self.parse_argument();
        self.load_img();
        self.process_img();
    }

    fn parse_argument(&mut self) {
        let args: Vec<String> = std::env::args().collect();
        if args.len() > 2 {
            self.source = args[1].clone();
            self.destination = args[2].clone();
        } else {
            println!("does not exist necessary argument");
            std::process::exit(0);
        }
    }

    fn load_img(&mut self) {
        if !self.is_exist_src() {
            println!("file does not exist.\nplease use path from what you are now");
            std::process::exit(0);
        }
        self.img = Some(ImageReader::open(&self.source).unwrap().decode().unwrap());
    }

    fn is_exist_src(&self) -> bool {
        let src = &(self.source);
        std::path::Path::new(src).exists()
    }

    fn process_img(&mut self) {
        let img = self.img.as_ref().unwrap();
        let img = img.blur(3.0);
        img.save(&self.destination);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?