Swap color channels

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
dlinaresg
Posts: 6
Joined: 2013-02-20T16:37:00-07:00
Authentication code: 6789

Swap color channels

Post by dlinaresg »

I´m trying to use Magick.net to interchange red and green channels in a RGB image, but cannot find swap method. I have found MagickImageCollection Reverse method which 'reverses the order of the images in the collection' but it´s not clear if rotates the channels (1 to 0, 2 to 1, 0 to 2) or, when there are three channels, swaps red channel (0) with green channel (2). Is Reverse the only way to swap two channels? How can I specify which channels to interchange?
Thanks
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: Swap color channels

Post by 246246 »

Get IMagickCollection by Separate(), reorder to the state whatever you want, then Combine();

Code: Select all

var x = image.Separate().ToList();
var y = new MagickImageCollection();
y.Add(x[2]);
y.Add(x[1]);
y.Add(x[0]);
var swapped = y.Combine();
dlinaresg
Posts: 6
Joined: 2013-02-20T16:37:00-07:00
Authentication code: 6789

Re: Swap color channels

Post by dlinaresg »

Thank you so much. Nice solution.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Swap color channels

Post by dlemstra »

I am also considering to add parts of https://imagemagick.org/script/command- ... channel-fx in some form to the API. Not sure how and when this will happen yet.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply