How to convert these IM commands to Magick.NET?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
Masejoer
Posts: 23
Joined: 2016-06-23T11:34:37-07:00
Authentication code: 1151

How to convert these IM commands to Magick.NET?

Post by Masejoer »

I have two Shell-script convert.exe commands I'm trying to convert over to use the .NET library. Can anyone provide guidance here?

First, I'm trying to remove the backdrop (trim doesn't work as well as this script does).

Part of shell-script:

Code: Select all

var=`convert ./input.jpg -blur 0x4 -threshold 45% -format %@ info:`
convert ./input.jpg -crop $var ./output.jpg
Blur and threshold in Magick.NET are easy, but not sure about the rest.

Code: Select all

Magick image = new Magick();
image = new MagickImage(input);    
image.Blur(0, 4);
image.Threshold(new ImageMagick.Percentage(45));
image.Write(output);
-----

On a different process, I've been trying to get the blue channel from the image to filter on, but I am unable to figure it out in Magick.NET. image2.Separate(Channels.Blue) isn't giving me what I expected.

Part of shell-script:

Code: Select all

convert ./input.jpg ( +clone -colorspace RGB -channel B -separate +channel -auto-level -threshold 16% +write x.png ) -compose Lighten -composite ./output.jpg
What I have so far in .NET:

Code: Select all

Magick image = new Magick();
image = new MagickImage(input);             
image2 = image.Clone();
image2.ColorSpace = ColorSpace.RGB;
//image2.Separate(Channels.Blue);
image.AutoLevel();
image.Threshold(new ImageMagick.Percentage(16));
//image2.Write("D:\\temp\\image2.png");
image.Composite(image2, CompositeOperator.Lighten);
image.Write(output);
Any help would be greatly appreciated!
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: How to convert these IM commands to Magick.NET?

Post by dlemstra »

Your commands would translate to this:

Code: Select all

using (MagickImage image = new MagickImage(input))
{
  image.Blur(0, 4);
  image.Threshold(new Percentage(45));
  var boundingBox = image.BoundingBox; // this is the output from -format %@ info:

  image.Read(input);
  image.Crop(boundingBox);
  image.Write(output);
}
And this:

Code: Select all

using (MagickImage image = new MagickImage(input))
{
  using (MagickImage clone = image.Clone())
  {
    clone.ColorSpace = ColorSpace.RGB;
    using (MagickImage blue = clone.Separate(Channels.Blue).First())
    {
      blue.AutoLevel();
      blue.Threshold(new Percentage(16));

      image.Composite(blue, CompositeOperator.Lighten);
      image.Write(output);
    }
  }
}
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply