This project has moved and is read-only. For the latest updates, please go here.

Can't Replicate Results Made by ImageMagick (CMD)

Oct 28, 2013 at 3:45 PM
I'm trying to replicate the instagram filters here in C#, but have run into multiple problems where the ImageMagick.NET analogs of the CMD options don't produce the same results.

For example, AutoGamma doesn't seem to work at all. Every time I call it on an image " img.AutoGamma()", the image gets over-saturated and becomes all white.

Lenna is turned into an all-white image by autogamma.

Example code:
MagickImage img = new MagickImage();

// Read from file
img.Read("D:\\lenna.jpg");
img.AutoGamma();
img.Write("D:\\lenna_out.jpg");
Another function that doesn't seem to be producing the same results is level. I'm trying to replicate "-level 33%" with the call "img.level(85,171)", using the description of how level works on the ImageMagick website, but the results are completely different than the cmd results.

Are there any docs I can look at to get more information about this? Are these functions not implemented the same in ImageMagick.NET as in the CMD version? Any help would be greatly appreciated.
Oct 28, 2013 at 4:07 PM
Edited Oct 28, 2013 at 4:40 PM
Maybe this is a HDRI bug? Is HDRI enabled in your version of convert?
Oct 28, 2013 at 7:57 PM
Edited Oct 28, 2013 at 9:03 PM
It doesn't seem to be enabled.

identify -version says:
Features: DPC OpenMP
With no mention of HDRI.

I did some more testing, and I think the problem is that AutoGamma() is not equal to AutoGamma(Channels.RGB). The former is what is causing the over-saturation, while I think the latter is what I really want. But I'm still not getting exactly the results that I want.

I also didn't realize I was working with 16 bit, so I was able to fix my level problem.

I have another question, though. How do I fill a DrawableRectangle with an rgba color? Specifically, I want to replicate:
-size imageWidth x imageHeight -fill rgba(255,153,0,0.5) -draw "rectangle 0,0,imageWidth,imageHeight" -compose multiply
Oct 29, 2013 at 6:59 AM
Edited Oct 29, 2013 at 4:18 PM
The autogamma problem is a bug in ImageMagick I will have to fux. With Channels.RGB & Channels.Opacity you will get the expected result. I will investigate this.

You can use the DrawableFillColor class to set the fill color. Make sure you pass all the drawables at once to the Draw method.
Oct 29, 2013 at 4:00 PM
Can you also confirm that imgA.Composite(imgB, CompisteOperator.Blend,args) is working properly? I think my results from Blend may be coming out wrong as well. Should args be a string containing, for example, "100,0" or does it need other elements?

Thanks for all your help.
Oct 29, 2013 at 5:56 PM
Edited Oct 29, 2013 at 6:29 PM
The args should have the value that you would normal set with -define compose:args (http://www.imagemagick.org/script/compose.php). Please provide me with command line example and a code example if you don't get the expected results.

The auto-gamma problem looks like a compiler bug, I will add a workaround for this in ImageMagick later this week.
Oct 29, 2013 at 6:37 PM
Edited Oct 29, 2013 at 6:43 PM
I'm trying to replicate:

inFile ( -clone 0 -fill color -colorize 100%% ) ( -clone 0 -colorspace gray -negate ) -compose blend -define compose:args= level,(100-level) -composite outfile

Where color (hexcode) is variable, level is always 100 (maintained as a variable for legacy reasons), and -negate is either used or not.

My code is:
MagickImage tempA = img.Clone();
tempA.Colorize(new MagickColor(color), new Percentage(100));
MagickImage tempB = img.Clone();
tempB.ColorSpace = ColorSpace.GRAY;
if (doNegate)
    tempB.Negate();
int la = level;
int lb = 100-level;
string args = la.ToString() + "," + lb.ToString();
tempA.Composite(tempB, 0, 0, CompositeOperator.Blend,args);
return tempA;
If there are &43;'s in there, they should be plus signs.
Oct 30, 2013 at 5:56 PM
Edited Nov 7, 2013 at 7:56 PM
I can reproduce part of the problem. I tried the following:
convert.exe inFile ( -clone 0 -fill white-colorize 100%% ) -compose blend -define compose:args=50,50 outfile
This should be the same as:
MagickImage img = new MagickImage("inFile");
MagickImage tempA = img.Clone();
tempA.Colorize(Color.White, new Percentage(100));
img.Composite(tempA, 0, 0, CompositeOperator.Blend, "50,50");
img.Write("outfile");
But this is giving me a completely white image, I will try with the latest version of ImageMagick from the subversion repository later this week and use some debugging to figure out what is happening.

Edit: I will look into this before the next release.