MagickImage.SparseColors() how does it work?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Posts: 6
Joined: 2014-07-03T03:05:05-07:00
Authentication code: 6789

MagickImage.SparseColors() how does it work?

Post by Paralon »

Hello there, everyone!
I couldn't find anything in Magick.NET documentation about SparseColors() method and it get's me confused.
I'm trying to recreate the effect of convert -sparse-color shepards "..." command line parameter. As far as I understand it gets a list of {X,Y COLOR} objects.

MagickImage.SparseColors() asks for Channel, SparseColorMethod and an array of double coordinates in his parameters.
What I did is the following:

Code: Select all

var coords = GetCoords(points);
image.SparseColor(Channels.All, SparseColorMethod.Shepards, coords);

Code: Select all

private double[] GetCoords(Point[] points)
    var list = new List<double>();
    for (var i = 0; i < points.Length; i++)
        var p = points[i];
        var c = colors[i];


    return list.ToArray();
And this code is not working (because colors are not specified, I guess). Question is how do I specify them?

User avatar
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: MagickImage.SparseColors() how does it work?

Post by fmw42 »

-sparse-color shepards takes a list of x,y,color and fills out the image using a shepards inverse distance squared interpolation see

sparse-color: outputs a list of x,y,color for every non-transparent (fully opaque) pixel in the image. see

I do not know if either are part of MagickNet, yet, as I am not a Windows user.

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

Re: MagickImage.SparseColors() how does it work?

Post by dlemstra »

It is quite tricky to specify the arguments so I have changed this for the next release. With the next release of Magick.NET you will be able to do this:

Code: Select all

  convert -size 600x60 xc: -sparse-color barycentric '0,0 skyblue  -%w,%h skyblue  %w,%h black' diagonal_gradient.jpg
MagickReadSettings settings = new MagickReadSettings();
settings.Width = 600;
settings.Height = 60;

using (MagickImage image = new MagickImage("xc:", settings))
  List<SparseColorArg> args = new List<SparseColorArg>();
  args.Add(new SparseColorArg(0, 0, new MagickColor("skyblue")));
  args.Add(new SparseColorArg(-image.Width, image.Height, new MagickColor("skyblue")));
  args.Add(new SparseColorArg(image.Width, image.Height, new MagickColor("black")));

  image.SparseColor(SparseColorMethod.Barycentric, args);
.NET + ImageMagick = Magick.NET, @MagickNET, Donate