From 9fb6664a631dff2e27f22d38ab247dd9378c31e1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 16 Dec 2020 17:46:17 +0100 Subject: [PATCH] Add inverting images and progressbar for image processing Signed-off-by: trivernis --- README.md | 2 +- src/utils/image_converting.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 181b9a9..c8495c0 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,7 @@ Hide a section (including subsections) in the TOC #[toc-hidden] Section Set the size of an image -!(url)[width = 42%, height=auto, grayscale, brightness=10, contrast=1.2] +!(url)[width = 42%, height=auto, grayscale, brightness=10, contrast=1.2, invert] Set the source of a quote [author=Me date=[[date]] display="{{author}} - {{date}}"]> It's me diff --git a/src/utils/image_converting.rs b/src/utils/image_converting.rs index 84e7cbf..874056e 100644 --- a/src/utils/image_converting.rs +++ b/src/utils/image_converting.rs @@ -4,6 +4,7 @@ use crate::utils::downloads::download_path; use image::imageops::FilterType; use image::io::Reader as ImageReader; use image::{GenericImageView, ImageFormat, ImageResult}; +use indicatif::{ProgressBar, ProgressStyle}; use mime::Mime; use parking_lot::Mutex; use rayon::prelude::*; @@ -46,12 +47,20 @@ impl ImageConverter { /// Converts all images pub fn convert_all(&mut self) { + let pb = Arc::new(Mutex::new(ProgressBar::new(self.images.len() as u64))); + pb.lock().set_style( + ProgressStyle::default_bar() + .template("Processing images: [{bar:40.cyan/blue}]") + .progress_chars("=> "), + ); self.images.par_iter().for_each(|image| { let mut image = image.lock(); if let Err(e) = image.convert(self.target_format.clone(), self.target_size.clone()) { log::error!("Failed to embed image {:?}: {}", image.path, e) } + pb.lock().tick(); }); + pb.lock().finish(); } } @@ -64,6 +73,7 @@ pub struct PendingImage { brightness: Option, contrast: Option, grayscale: bool, + invert: bool, } impl PendingImage { @@ -78,6 +88,7 @@ impl PendingImage { brightness: None, contrast: None, grayscale: false, + invert: false, } } @@ -89,6 +100,7 @@ impl PendingImage { self.contrast = Some(contrast as f32); } self.grayscale = meta.get_bool("grayscale"); + self.invert = meta.get_bool("invert"); } /// Converts the image to the specified target format (specified by target_extension) @@ -147,6 +159,9 @@ impl PendingImage { if self.grayscale { image = image.grayscale(); } + if self.invert { + image.invert(); + } let data = Vec::new(); let mut writer = Cursor::new(data); @@ -191,6 +206,7 @@ impl PendingImage { if let Some(c) = self.contrast { file_name += &*format!("-{}", c); } + file_name += &*format!("{}-{}", self.invert, self.grayscale); file_name += format!("-{}", type_name).as_str(); path.set_file_name(file_name);