Compose with -channel (command line to c#)

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
Glimtch
Posts: 2
Joined: 2019-02-05T08:01:01-07:00
Authentication code: 1152

Compose with -channel (command line to c#)

Post by Glimtch » 2019-02-05T08:38:02-07:00

Hello everyone! I'm sort of new to ImageMagick as whole and I would like to ask for your help.

I need to apply certain filters to the images (photos) as if it was in instagram or in a photoshop alike app. Filters come with different settings such as sepia, hue, gradients and compose modes. Where I get a problem is a multiply type of composition.

My filter is a half-transparent gradient such as this one (square):
Image

Taking this example image (also square and same size):
Image

Applying this command

Code: Select all

magick convert Image.png Overlay.png -compose Multiply -composite Result1.png
will result in a half-transparent image as the alpha channel is also multiplyed throughout the image:
Image

Of course, this is not what I am looking for so the solution is to specify the -channel parameter allowing to 'skip' the alpha

Code: Select all

magick convert Image.png Overlay.png -channel rgb -compose Multiply -composite Result2.png
Result as expected and correct:
Image

But the MagickImage class in .NET does not have a Composite method overload that accepts Channels enum as a parameter.
So this line

Code: Select all

magickImage.Composite(overlay, CompositeOperator.Multiply);
by default multiplies alpha as well resulting in the first example.

So I would really appreciate if you could help me with this one.

P. S.

Multiplying as is and then removing transparency may look like a decent workaround at first, but there is a catch.
If an original image has its own transparency (even a small part of it does) like this
Image

Then after composition you can't tell what part was transparent in the beginning and what got its alpha by multiplying a filter.
So if removed transparency at that point it will also lose the one it was supposed to have:
Image

User avatar
dlemstra
Posts: 1568
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Compose with -channel (command line to c#)

Post by dlemstra » 2019-02-05T23:27:34-07:00

Thanks for finding this. Could you open an issue at "https://github.com/dlemstra/Magick.NET/issues" so I don't forget adding support for this?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

User avatar
dlemstra
Posts: 1568
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Compose with -channel (command line to c#)

Post by dlemstra » 2019-02-10T07:50:20-07:00

Just created the issue myself and made some changes to Magick.NET. Your feature request will be available in the next release.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

Glimtch
Posts: 2
Joined: 2019-02-05T08:01:01-07:00
Authentication code: 1152

Re: Compose with -channel (command line to c#)

Post by Glimtch » 2019-02-10T22:27:51-07:00

Thank you.
In any case, if someone is wondering, I came up with a workaround on this one by saving the alpha of the image and then applying it later:

Code: Select all

MagickImage alpha = (MagickImage)magickImage.Separate(Channels.Alpha).First();
magickImage.Composite(overlay, CompositeOperator.Multiply);
magickImage.Composite(alpha, CompositeOperator.CopyAlpha);

Post Reply