diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2022-11-05 11:44:58 +0100 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2022-11-05 11:44:58 +0100 |
| commit | 69336e9c603608e27cc4458d664839f038233a00 (patch) | |
| tree | 954e1d82c44385e4ec3c54a22c71511c94b6069f /src | |
| parent | fefb0e01c42c74a98aafed4ebf99e2eebed0a2c4 (diff) | |
Add resizing for work card images
Diffstat (limited to 'src')
| -rw-r--r-- | src/images_processing.rs | 24 | ||||
| -rw-r--r-- | src/main.rs | 2 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/images_processing.rs b/src/images_processing.rs new file mode 100644 index 0000000..483d44c --- /dev/null +++ b/src/images_processing.rs @@ -0,0 +1,24 @@ +use anyhow::{Context, Result}; + +// resize all work card images to 250 x 250 px +pub fn resize_workcard_images() -> Result<Vec<()>> { + let mut result: Vec<()> = Vec::new(); + for image_file in std::fs::read_dir("images/workcardimages")? { + let path = image_file?.path(); + let filename = if let Some(name) = &path.to_str() { + Ok(format!("docs/{}", name)) + } else { + Err(anyhow::Error::msg("")) + }; + + let img = image::open(&path)?; + let processed_image = img.resize(250, 250, image::imageops::FilterType::Triangle); + let mut output_file = std::fs::File::create(filename?)?; + result.push( + processed_image + .write_to(&mut output_file, image::ImageFormat::Png) + .with_context(|| format!("Failed to resize work card image from {:?}", path))?, + ); + } + Ok(result) +} diff --git a/src/main.rs b/src/main.rs index 942b621..39d4b9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,11 @@ use tera::{Context, Tera}; use crate::works::Works; +mod images_processing; mod works; fn main() -> Result<()> { + images_processing::resize_workcard_images()?; // new tera instance let tera = Tera::new("templates/**/*.html")?; |
