Skip to content

Commit

Permalink
feat: Add argument for specifying output path.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhelvetican authored May 15, 2024
1 parent a09d27d commit 84014a9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
[package]
name = "glassy"
authors = ["decipher", "Rhelvetican"]
version = "0.1.1"
version = "0.1.2"
edition = "2021"

[dependencies]
anyhow = "1"
image = "0.25"
imageproc = "0.24"
thiserror = "1.0"
clap = { version = "4.5", features = ["derive"] }
libblur = "0.9"
log = "0.4"
env_logger = "0.11"

[profile.release]
lto = "fat"
opt-level = 3
codegen-units = 1
rpath = true
debug = false
panic = "abort"
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ It adds variable blur and noise to image as overlay to give them a glassy look.

## Usage

```
```sh
~ $ glassy -h

A simple CLI tool to apply glass-like overlay effect to images

Usage: glassy [OPTIONS] <PATH>
Usage: glassy.exe [OPTIONS] <PATH>

Arguments:
<PATH> Path to image file
Expand All @@ -20,6 +20,8 @@ Options:
Strength of the glass effect [default: medium] [possible values: low, medium, high]
--no-grain
Apply effect without grain
-o, --output <PATH>
Specify output file path
-v, --verbose
Explain what is being done
-h, --help
Expand Down
4 changes: 1 addition & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use image::ImageError as ImgError;
use std::{io::Error as IoError, result::Result as StdResult};
use std::io::Error as IoError;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -9,5 +9,3 @@ pub enum Error {
#[error("Invalid image file")]
DecodeError(#[from] ImgError),
}

pub type Result<T> = StdResult<T, Error>;
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use clap::Parser;

use crate::utils::{cli::CliArgs, proc_image};
use std::process;
use clap::Parser;
use log::error;
use std::process::exit;

mod utils;
mod error;
mod logger;
mod utils;

fn main() {
let cli_args: CliArgs = CliArgs::parse();

logger::init_logger(&cli_args);

if let Err(e) = proc_image(cli_args) {
log::error!("{e}");
process::exit(1);
error!("{e}");
exit(1);
};
}
4 changes: 4 additions & 0 deletions src/utils/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub struct CliArgs {
#[arg(long = "no-grain")]
pub no_grain: bool,

/// Specify output file path
#[arg(short, long, value_name = "PATH", value_hint = ValueHint::FilePath)]
pub output: Option<String>,

/// Explain what is being done
#[arg(short, long)]
pub verbose: bool,
Expand Down
28 changes: 20 additions & 8 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::error::Result;
use anyhow::Result;

use self::{blur::add_blur, noise::add_noise};
use image::io::Reader;
use log::info;
use std::path::Path;
use std::{fs::DirBuilder, path::Path};

mod blur;
pub mod cli;
Expand All @@ -27,12 +27,24 @@ pub fn proc_image(cli_args: cli::CliArgs) -> Result<()> {
add_noise(img, noise_opts)
};

let output_path = format!(
"{}_{}.{}",
path.file_stem().unwrap().to_str().unwrap_or("Image"),
cli_args.effect_strength,
path.extension().unwrap().to_str().unwrap_or("png")
);
let output_path = if let Some(output) = cli_args.output {
output
} else {
format!(
"{}_{}.{}",
path.file_stem().unwrap().to_str().unwrap_or("Image"),
cli_args.effect_strength,
path.extension().unwrap().to_str().unwrap_or("png")
)
};

// Create the output directory if it doesn't exist
let output = Path::new(output_path.as_str());
if !output.exists() {
DirBuilder::new()
.recursive(true)
.create(output.parent().unwrap())?;
}

info!("Saving Image: {output_path}");

Expand Down

0 comments on commit 84014a9

Please sign in to comment.