From b4442b61fe90da279f5cfa58e53ff0f5262db694 Mon Sep 17 00:00:00 2001 From: Timo Uelen Date: Wed, 7 Aug 2024 16:00:39 +0200 Subject: [PATCH 1/2] Add an example to add a border --- examples/add-border.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/add-border.rs diff --git a/examples/add-border.rs b/examples/add-border.rs new file mode 100644 index 0000000..2db4b82 --- /dev/null +++ b/examples/add-border.rs @@ -0,0 +1,33 @@ +extern crate magick_rust; +use magick_rust::{magick_wand_genesis, CompositeOperator, MagickError, MagickWand, PixelWand}; +use std::fs; +use std::sync::Once; + +// Used to make sure MagickWand is initialized exactly once. Note that we do not +// bother shutting down, we simply exit when we're done. +static START: Once = Once::new(); + +// Read the named file and add a 10 pixel border around the image +fn add_border(filepath: &str, border_color: &str) -> Result, MagickError> { + START.call_once(|| { + magick_wand_genesis(); + }); + + let wand = MagickWand::new(); + wand.read_image(filepath)?; + + let mut border = PixelWand::new(); + border.set_color(border_color)?; + + wand.border_image(&border, 10, 10, CompositeOperator::Over)?; + wand.write_image_blob("jpeg") +} + +fn main() { + match add_border("../tests/fixtures/snow-covered-cat.jpg", "red") { + Ok(bytes) => { + fs::write("border-cat.jpg", bytes).expect("write failed"); + } + Err(err) => println!("error: {}", err), + } +} From 7029d040b27465632ba2998403a7603643cd5c4d Mon Sep 17 00:00:00 2001 From: Timo Uelen Date: Wed, 7 Aug 2024 16:03:40 +0200 Subject: [PATCH 2/2] Fix path --- examples/add-border.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/add-border.rs b/examples/add-border.rs index 2db4b82..b618542 100644 --- a/examples/add-border.rs +++ b/examples/add-border.rs @@ -24,7 +24,7 @@ fn add_border(filepath: &str, border_color: &str) -> Result, MagickError } fn main() { - match add_border("../tests/fixtures/snow-covered-cat.jpg", "red") { + match add_border("tests/fixtures/snow-covered-cat.jpg", "red") { Ok(bytes) => { fs::write("border-cat.jpg", bytes).expect("write failed"); }