CompositeOperator enum

Nov 7, 2014 at 11:22 PM
I have been trying out Magick.Net this week and have found it very useful. I ran into a problem trying to do a command matching the following operation using the command line tools.

convert infile.jpg circle_mask.png -alpha Off -compose CopyOpacity -composite outfile.png

The problem I ran into is that the CompositeOperator enum doesn't have the CopyOpacity option. Is that a mistake, or is there some other way to accomplish the same thing?
Coordinator
Nov 8, 2014 at 9:13 AM
Edited Nov 10, 2014 at 6:36 PM
The CopyOpacity has been renamed to CopyAlpha in ImageMagick 7. But that is probably not what you want.

ImageMagick does some magic when the input image has no alpha channel and you compose with CopyOpacity/CopyAlpha. It uses the circle_mask.png file as a mask for the image. The -alpha off forces this behavior (you can find a good explanation here: http://www.imagemagick.org/Usage/compose/#copyopacity. This means the command actually translates to this:
using (MagickImage img= new MagickImage("infile.gif"))
{
  using (MagickImage mask = new MagickImage("circle_mask.png"))
  {
    using (MagickImage outfile = new MagickImage(MagickColor.Transparent, img.Width, img.Height))
    {
      img.Mask = mask;
      outfile.Composite(img, Gravity.Center, CompositeOperator.SrcOver);
      outfile.Write("outfile.png");
    }
  }
}
Nov 10, 2014 at 4:43 PM
Thanks so much for the quick reply and thorough example. I will give that a try today.
Nov 10, 2014 at 9:59 PM
Your suggestion worked perfectly - thank you. Just one thought - you might make a note somewhere on the main page about Magick.Net being built on version 7 of imagemagick. You do have the Magick.Net version listed as Magick.NET 7.0.0.0007, but not having used Imagemagick for awhile I didn't realize that correlated to a new version of imagemagick. The info about version 7 is pretty buried on the Imagemagick site. Just my 2 cents.
Coordinator
Nov 11, 2014 at 9:23 PM
Thanks for the feedback, I have added a small part about ImageMagick 7 to the documentation page.
Nov 12, 2014 at 9:47 PM
So it turns out that I still am having a little problem with this. It worked great on a mask that was On/Off values, but with a variable mask my result is a lot different from IM 6, which may be correct but not what I'm after. Here are my samples, any idea on what I could do different? Thanks for any input you may have.

IM 6 command line -

convert alphatemp1.png alphatemp2.png -alpha off -compose copy_opacity -composite final_im6.png

Magick.Net code

 using (MagickImage img = new MagickImage(TempFile1))
            {
                using (MagickImage mask = new MagickImage(TempFile2))
                {
                    using (MagickImage outfile = new MagickImage(MagickColor.Transparent, img.Width, img.Height))
                    {
                        img.Mask = mask;
                        outfile.Composite(img, 0,0 , CompositeOperator.SrcOver);
                        outfile.Write("final.png");
                    }
                }
            }
Input Image
Image
Mask Image
Image

IM6 copy_opacity output
Image

Magick.Net output (you can't tell the difference as much on the web page, but in a view that shows transparent you can really see how much additional artifact is left)
Image
Coordinator
Dec 9, 2014 at 4:25 PM
This might be a bug in ImageMagick I will take a look at it when I am back next week,
Coordinator
Dec 22, 2014 at 9:01 PM
It seems I made a mistake when translating your command to code. You don't need to set a Mask. The following works in my development build:
using (MagickImage img = new MagickImage("alphatemp1.png"))
{
  using (MagickImage mask = new MagickImage("alphatemp2.png"))
  {
    img.Composite(mask, 0, 0, CompositeOperator.CopyAlpha);
    img.Write("final_im7.png");
  }
}
You will get the same output as IM6 in the next release of Magick.NET
Dec 29, 2014 at 4:43 PM
Thanks, I see you released an update so I hope to get back and give this a try this week.