nlfiedler / magick-rust

Rust bindings for ImageMagick
https://crates.io/crates/magick_rust
Apache License 2.0
246 stars 63 forks source link

Subcommands Usage #81

Open LeibnizCapital opened 3 years ago

LeibnizCapital commented 3 years ago

How would I go about doing this?

magick $1 -crop \
    `magick $1 -virtual-pixel edge -blur 0x15 -fuzz 15% \
             -trim -format '%wx%h%O' info:`   +repage   $2

I'm not looking for an exact answer, just how to approach the problem. I assume two distinct wand, but I'd like to see an example.

LeibnizCapital commented 3 years ago

I could swear I got the right syntax but it does not work. The command above actually trims the image which these instructions do not:

    START.call_once(|| {
        magick_wand_genesis();
    });
    let wand = MagickWand::new();
    wand.read_image(file_path).unwrap();
    wand.fit(414, 896);
    let scaled_img = wand.write_image_blob("JPEG").unwrap();
    // Trimming a blurred version of the image to catch the border correctly
    let wand_trim = MagickWand::new();
    wand_trim.read_image_blob(scaled_img).unwrap();
    wand_trim.blur_image(0_f64, 15.0).unwrap();
    wand_trim.trim_image(0.15).unwrap();
    // Same as +repage option: https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=23397
    wand_trim.reset_image_page("0x0+0+0").unwrap();
    debug_assert_ne!(wand.get_image_width(), wand_trim.get_image_width());

I can see the same blur if I write the image, but no trim. What might I be missing?

nlfiedler commented 3 years ago

Unfortunately, this is well beyond any usage with which I have experience. One question though, does the -crop in the command example have a relevant effect? I don't see a similar operation in the Rust code, though.

LeibnizCapital commented 3 years ago

There is a wand.crop_image(..) which is equivalent to crop. The problem occurs before. We blur a copy of our scan to remove make the noisy border into a uniform color then trim it. That's why we debug_assert_ne expecting the trimmed copy to be smaller as it is when running the command. Then, we crop our original scan using the dimension of the trimmed blurred copy to remove the same border: https://legacy.imagemagick.org/Usage/crop/#trim_blur. There may be a better way to do this with V7.