Implemented some edit functions

Now the user can blur, huerotate, grayscale, invert, brighten and contrast an image (Including automatic conversion between formats)
This commit is contained in:
Víctor Martínez 2020-11-22 23:20:21 +01:00
parent 9b4c3f588f
commit 21d071fc74
3 changed files with 91 additions and 11 deletions

BIN
mock/hola.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

View file

@ -25,33 +25,77 @@ pub fn build_cli() -> App<'static, 'static> {
// OPTIONALS GROUP, AT LEAST ONE OPTION IS REQUIRED
.group(
ArgGroup::with_name("options")
.required(true)
.multiple(true)
.args(&["blur", "huerotate", "brighten", "contrast"]),
.required(true)
.multiple(true)
)
.group(
ArgGroup::with_name("colors")
.required(false)
.multiple(false)
)
// OPTIONAL ARGUMENTS
.arg(
Arg::with_name("blur")
.long("blur")
.help("Apply blur to the image")
.group("options")
.validator(_validate_float)
.takes_value(true)
.help("Apply blur to the image"),
)
.arg(
Arg::with_name("huerotate")
.long("hue-rotate")
.help("Rotate the hue color scale of the image")
.group("options")
.group("colors")
.validator(_validate_integer)
.takes_value(true)
.help("Rotate the hue color scale of the image"),
)
.arg(
Arg::with_name("brighten")
.long("brighten")
.help("Sets the brighten of the image")
.group("options")
.validator(_validate_integer)
.takes_value(true)
.help("Sets the brighten of the image"),
)
.arg(
Arg::with_name("contrast")
.long("contrast")
.help("Apply contrast to the image")
.group("options")
.validator(_validate_float)
.takes_value(true)
.help("Apply contrast to the image"),
)
.arg(
Arg::with_name("grayscale")
.long("grayscale")
.short("G")
.help("Apply grayscale filter to the image")
.group("options")
.group("colors")
)
.arg(
Arg::with_name("invert")
.long("invert")
.short("I")
.help("Apply Invert colors filter")
.group("colors")
.group("options")
)
}
fn _validate_integer(arg: String) -> Result<(), String> {
match arg.parse::<i32>() {
Ok(_) => Ok(()),
Err(_) => Err(String::from("Type Error: Integer expected"))
}
}
fn _validate_float(arg: String) -> Result<(), String> {
match arg.parse::<f32>() {
Ok(_) => Ok(()),
Err(_) => Err(String::from("Type Error: Float expected"))
}
}

View file

@ -1,13 +1,49 @@
use image::{DynamicImage, open};
use lib::{cli};
use clap::ArgMatches;
use image::{open, DynamicImage};
use lib::cli;
fn main() {
let matches = cli::build_cli().get_matches();
let input = matches.value_of("INPUT").unwrap();
let output = matches.value_of("OUTPUT").unwrap();
let image = open(input).unwrap();
image.save(output).unwrap();
match open(input) {
Ok(image) => {
let image = edit_photo(image, &matches);
if let Err(error) = image.save(output) {
println!("ImageError: {}", error);
};
}
Err(error) => println!("Image Error: {}", error),
};
}
fn edit_photo(mut image: DynamicImage, matches: &ArgMatches) -> DynamicImage {
// APPLY BLUR
if let Some(sigma) = matches.value_of("blur") {
image = image.blur(sigma.parse().unwrap());
}
// APPLY CONTRAST IF MATCHES
if let Some(value) = matches.value_of("contrast") {
image = image.adjust_contrast(value.parse().unwrap());
}
// APPLY BRIGHTEN IF MATCHES
if let Some(value) = matches.value_of("brighten") {
image = image.brighten(value.parse().unwrap());
}
// APPLY HUE ROTATE IF MATCHES
if let Some(degrees) = matches.value_of("huerotate") {
image = image.huerotate(degrees.parse().unwrap());
}
// GRAY SCALE
if matches.is_present("grayscale") {
image = image.grayscale();
}
// INVERT COLORS
if matches.is_present("invert") {
image.invert();
}
image
}