Compare - Mimic Command Line Function

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
craig6874
Posts: 3
Joined: 2015-03-30T17:15:09-07:00
Authentication code: 6789

Compare - Mimic Command Line Function

Post by craig6874 » 2015-03-30T17:49:29-07:00

Hello All,

I'm using Magick.NET (Q16-x64 v7.0.0.0011) to compare images. When I use the command line version of ImageMagick and do a compare without any special options, it gives an image with the identical portions shown as a lightened background and the differences in red. I'm trying to duplicate this behavior in Magick.NET. I tried the following code:

Code: Select all

var image1Path = @"D:\CompareTest\image1.png";
var image2Path = @"D:\CompareTest\image2.png";

var diffImagePath = @"D:\CompareTest\imageDIFF.png";
			
using (MagickImage image1 = new MagickImage(image1Path))
using (MagickImage image2 = new MagickImage(image2Path))
{
  var diffImage = new MagickImage();

  image1.Compare(image2, ErrorMetric.Absolute, diffImage);

  diffImage.Write(diffImagePath);
}

What I end up with though is a file that shows only the differences. This seems like what you would get if you ran the command line version with "-compose src". The differences are whatever SetHighlightColor is set to and the rest of the image is a solid color according to SetLowlightColor. I tried several different files and file formats with the same result.

Maybe the answer is that I need to assemble that final image myself by combining the originals and the diff. I'm not having much luck with that either. Any help would be greatly appreciated.

-Craig

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

Re: Compare - Mimic Command Line Function

Post by dlemstra » 2015-03-31T04:22:53-07:00

You need to set the Compose property of image1 to CompositeOperator.Over to get the same behavior as the command line. But it looks like this is bugged in IM7. We are investigating this.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

craig6874
Posts: 3
Joined: 2015-03-30T17:15:09-07:00
Authentication code: 6789

Re: Compare - Mimic Command Line Function

Post by craig6874 » 2015-03-31T05:10:18-07:00

Dlemstra,

Thanks for your reply and development on this great wrapper. I verified that setting the compose property to CompositeOperator.Over gives the desired result on version 6.8.9.601 but not 7.0.0.0011.

Is there a way to darken just the background image a bit in the resulting diff image? In some of my tests, the background image is there, it's just extremely light and hard to see.

Thanks Again!

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

Re: Compare - Mimic Command Line Function

Post by dlemstra » 2015-03-31T07:02:10-07:00

You probably don't want to set the Composite property then (I will add an extra overload to Compare in the next release to make settings this easier). You probably want to Composite the diffImage on top of image1. You should do something like this:

Code: Select all

image1.Compare(image2, ErrorMetric.Absolute, diffImage);
image1.Composite(diffImage, CompositeOperator.Over);
image1.Write(diffImagePath);
This will allow you to modify image1 before you draw the diffImage over it or you could use a different CompositeOperator to get a different result.

p.s. I will post an answer on your StackOverflow post when the new release has been published.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

craig6874
Posts: 3
Joined: 2015-03-30T17:15:09-07:00
Authentication code: 6789

Re: Compare - Mimic Command Line Function

Post by craig6874 » 2015-03-31T07:39:42-07:00

Awesome, Thank you.

Post Reply