This project has moved. For the latest updates, please go here.

Convert command to .NET

Jan 13, 2015 at 9:32 AM
Edited Jan 13, 2015 at 9:53 AM
This convert command should render a pencil sketch from the source image.

convert source.jpg
-clone 0 -modulate 100,0,100
-clone 1 -negate -blur 0x4
-clone 1 -clone 2 -compose color_dodge -composite -level 20x100%
-clone 3 -alpha set -channel a -evaluate set 100% +channel
-clone 3 -clone 4 -compose multiply -composite
-clone 0 -modulate 100,100,100
-delete 0-4 -compose screen -composite dest.jpg

I want to use the .NET API instead and tried the following. Tried reading documents and googled, but it seems my understanding on how to convert it is lacking anyhow as it only results in a black image.

var image = new MagickImage("source.jpg");
var clone0 = image.Clone();
clone0.Modulate(100, 0, 100);
var clone2 = clone0.Clone();
clone2.Compose = CompositeOperator.ColorDodge;
clone2.Composite(clone0, Gravity.Center);
clone2.Level(20, 100);
var clone3 = clone2.Clone();
clone3.Evaluate(Channels.All, EvaluateOperator.Set, 100);
var clone4 = clone3.Clone();
clone4.Compose = CompositeOperator.Multiply;
clone4.Composite(clone3, Gravity.Center);
clone0.Modulate(100, 100, 100);
clone0.Compose = CompositeOperator.Screen;
clone0.Composite(clone4, Gravity.Center);

Can someone help out or give pointers in the right direction (or is there a better way to solve this, like converting to MagickScript somehow)?
Jan 13, 2015 at 10:00 PM
Edited Jan 13, 2015 at 10:01 PM
Below is an how your command can be translated to Magick.NET. This is not the most efficient way to do this but it should help you to translate commands in the future. I have added a comment with the 'imagelist' at different points in the operation so you can better understand which image is being cloned. You probably also want to take a look at the Sketch method of MagickImage., This produces different results but might be interesting for you.
MagickImage logo = new MagickImage("logo:");

// ( -clone 0 -modulate 100,0,100 )
MagickImage clone0 = logo.Clone();
clone0.Modulate(100, 0, 100);

// imagelist: logo, clone0

// ( -clone 1 -negate -blur 0x4 )
MagickImage clone1 = clone0.Clone();
clone1.Blur(0, 4);

// imagelist: logo, clone0, clone1

// ( -clone 1 -clone 2 -compose color_dodge -composite -level 20x100% )
MagickImage compositeClone1 = clone0.Clone();
MagickImage compositeClone2 = clone1.Clone();
compositeClone1.Composite(compositeClone2, 0, 0, CompositeOperator.ColorDodge);
compositeClone1.Level((ushort)(Quantum.Max * 0.2), Quantum.Max);

compositeClone2.Dispose(); // composite produces 1 image and discards the other

// imagelist: logo, clone0, clone1, compositeClone1

// ( -clone 3 -alpha set -channel a -evaluate set 100% +channel )
MagickImage alpha = compositeClone1.Clone();
alpha.Evaluate(Channels.Alpha, EvaluateOperator.Set, 0);

// imagelist: logo, clone0, clone1, compositeClone1, alpha

// ( -clone 3 -clone 4 -compose multiply -composite )
MagickImage compositeClone3 = compositeClone1.Clone();
MagickImage compositeClone4 = alpha.Clone();
compositeClone3.Composite(compositeClone4, 0, 0, CompositeOperator.Multiply);

// side note: In the next version of Magick.NET you will be able to do this:
// compositeClone3.Composite(compositeClone4, CompositeOperator.Multiply);


// imagelist: logo, clone0, clone1, compositeClone1, alpha, compositeClone3

// ( -clone 0 -modulate 100,0,100 )
MagickImage clone0_2 = logo.Clone();
clone0_2.Modulate(100, 100, 100);

// imagelist: logo, clone0, clone1, compositeClone1, alpha, compositeClone3, clone0_2

// -delete 0-4

// imagelist: compositeClone3, clone0_2

// -compose screen -composite logo.png
compositeClone3.Composite(clone0_2, 0, 0, CompositeOperator.Screen);
Jan 15, 2015 at 11:13 AM
Makes a world of difference to understand how clone and composite works as well. Thanks a lot for the detailed explanation and solution!