Composite ImageMagick equivalent in Magick.NET

Jul 24, 2013 at 10:00 AM
Edited Jul 24, 2013 at 10:07 AM
Hello,

I try to port this command to Magick.NET :
convert myimage.jpg -set colorspace RGB
        ( -clone 0 -fill #ff0000 -colorize 100% )
        ( -clone 0 -colorspace gray -negate)
        -compose blend -define compose:args=100,0 -composite result.jpg
I try this :
            //img is my original image
            MagickImage ret = img.Copy();

            MagickImage filter1 = img.Copy();

            filter1.Colorize(color, new Percentage(100));

            MagickImage filter2 = img.Copy();

            filter2.ColorSpace = ColorSpace.GRAY;
            filter2.Negate();
How to combine all of this to obtain the same result of ImageMagick command?

Thank you a lot ! :)
Coordinator
Jul 24, 2013 at 9:01 PM
Edited Jul 26, 2013 at 8:00 AM
At this moment it is not possible to set the 'compose:args=100,0'. I will have to submit a patch to ImageMagick to make this available in Magick++. The method you are looking for is img.Composite() but without the 'compose:args' setting the operator CompositeOperator.Blend does nothing.

Edit:
My patch has been accepted. I will add an extra argument to the Composite method to set the 'compose:args'.
Jul 26, 2013 at 8:26 AM
Good news !!!

Thanks a lot for your great job!
Coordinator
Jul 27, 2013 at 12:34 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jun 19, 2014 at 5:39 PM
Edited Jun 19, 2014 at 5:42 PM
Hello Is it possible to write the following command line using Magick.NET ?
convert myimage.jpg -set colorspace RGB
        ( -clone 0 -fill #ff0000 -colorize 100% )
        ( -clone 0 -colorspace gray -negate)
        -compose blend -define compose:args=100,0 -composite result.jpg
I have tried to merge 3 MagickImage using this syntax to create a colortone function but no success :'(
img.Composite(filter1, Gravity.Center, CompositeOperator.Blend, args);
img.Composite(filter2, Gravity.Center, CompositeOperator.Blend, args);
Please could you drive me to the solution by giving right syntaxe?

Thank you very much!
Jun 19, 2014 at 5:48 PM
Did you find how to do it? I tried but I didn't find the right syntax? Please could you give me a piece of code to make me sleep in peace again :)
Coordinator
Jun 19, 2014 at 9:54 PM
I cannot reproduce the same result as the command line. I will have to debug ImageMagick in VisualStudio to figure out what is happening. I will get back to this later.
Jun 20, 2014 at 7:17 AM
Edited Jun 20, 2014 at 7:18 AM
Thank you for your responding so fast.
After reading my post again I feel like it looks a bit confused, just to be sure about the issue :
I need to compose 3 images into one using Magick.NET C# syntaxe.

Sorry for my english :)
Coordinator
Jun 20, 2014 at 7:59 AM
You can compose 3 images with Magick.NET and the correct syntax is indeed 'img.Composite(filter2, Gravity.Center, CompositeOperator.Blend, args);'. But the current release contains a bug so this will not work. The bug will be fixed in the next release that will be published soon.
Jun 26, 2014 at 8:16 AM
Edited Jun 26, 2014 at 8:21 AM
I downloaded last release and I tried to make it work for two days but no success :(
Maybe I am missing something but I don't know how to compose 3 images using Magick.NET.
Starting with Trax63 code :
           var args = "100,0";
           MagickImage ret = img.Copy();

            MagickImage filter1 = img.Copy();

            filter1.Colorize(color, new Percentage(100));

            MagickImage filter2 = img.Copy();

            filter2.ColorSpace = ColorSpace.GRAY;
            filter2.Negate();
What should I write to get the final result? I tried many combination but it did not work, If I just write only what you give me :
img.Composite(filter2, Gravity.Center, CompositeOperator.Blend, args);
I guess I'm missing something...but cannot figure it out.
Coordinator
Jun 26, 2014 at 9:11 PM
Edited Jun 27, 2014 at 4:41 AM
You are missing a property that is not yet available in Magick.NET. I stepped trough ImageMagick in Visual Studio to figure out what is happening. And it looks like there is a also bug in the OpenCL code.

I almost got it working excepted for the '-set colorspace RGB' option. I will get back to you about this later.
Jun 27, 2014 at 1:43 PM
Edited Jun 27, 2014 at 1:51 PM
I though the bug was solved in the last released, I will wait for your reply :)
As you could guess I'm a beginnner with ImageMagick, I am not using the '-set colorspace RGB' actually, the exact command line I am trying to translate into c# is this one :
convert imgInput.jpg ( -clone 0 -fill #330000 -colorize 100% ) ( -clone 0 -colorspace gray -negate ) -compose blend -define compose:args=100,0 -composite imgOutput.jpg
According to ImageMagick :
  • imgInput is the background (dest)
  • "-clone 0 -fill #330000 -colorize 100%" is the overlay (src)
  • "-clone 0 -colorspace gray -negate" is the mask
So the right syntax should be (to me):
            var dest = input.Clone();

            var src = input.Clone();
            src.Colorize(new MagickColor(hexaColor), new Percentage(100));

            var mask= input.Clone();
            mask.ColorSpace = ColorSpace.GRAY;
            mask.Negate();

            dest.ClipMask = mask;

            dest.Composite(src, Gravity.Center, CompositeOperator.Blend, "100,0");
            dest.Write("imgOutput.jpg");
I will wait for you and try to be helpful somehow :)
Good luck!!!
Coordinator
Jun 27, 2014 at 2:46 PM
Edited Jun 27, 2014 at 6:59 PM
I am sorry for the misunderstanding and I will try to publish a new release this weekend. I had almost the same code as you just posted, the correct code will be this:
// You don't need to clone the input image.
//var dest = input.Clone(); 

using (var src = input.Clone())
{
  // There is an implicit cast from an integer to a Percentage.
  src.Colorize(new MagickColor(hexaColor), 100);

  using (var mask = input.Clone())
  {
    mask.ColorSpace = ColorSpace.GRAY;

    // Do not negate the image because the command line does that internally and the -negate is
    // there to 'double negate' (make it normal). The Magick.NET codes skips that part.
    // mask.Negate();

    // This will be the new property that works differently from ClipMask
    input.Mask = mask;

    input.Composite(src, Gravity.Center, CompositeOperator.Blend, "100,0");
    input.Write("imgOutput.jpg");
  }
}
Jun 29, 2014 at 7:05 AM
Thank you for the next release, I'll test it as soon as it is ready.
Thank you for your efficiency!