Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Map ImageMagick commands to .net code using Magick.net

Let's say I want to transform the transparent background of an image into white (flatten a png): A simple google search points me to: convert -flatten image.png noTransparency.png. This works fine if I invoke imageMagick from the command line. But let's say I want to use the .NET wrapper magick.net to do the same thing. Neither Google nor magick.net's documentation is very helpful for this case.

Same thing happens with convert original.png +transparent "#E6B15A" singleColor.png (A way to create a new image containing only a specific color from the original) I can't find ANY way to do this using the magick.NET wrapper

Why is so hard to map imageMagick commands into .net code using magick.net??

Is there any way I could use magick.net to execute imageMagick scripts instead? I could easily create a cmd Process from .net and call imageMagick commands from there, but if there is a wrapper it must be for some reason... Is there any good and extensive magick.net documentation that I'm missing?

like image 744
Alejandro Lozdziejski Avatar asked Jul 22 '15 02:07

Alejandro Lozdziejski


1 Answers

I am sorry but there is no extensive version of the documentation available at this moment. I have only created a couple of examples and documented them on the website. I don't have the time/team to create more and better documentation. But I can help you with the answer to both questions.

convert -flatten image.png noTransparency.png

This should actually be written like this:

convert image.png -flatten noTransparency.png

First image.png is read and then it is flattened and written to noTransparency.png. But in ImageMagick it 'secretly' adds another image that has the same size as image.png and uses that to flatten image.png. You can write it like this in Magick.NET:

using (MagickImageCollection images = new MagickImageCollection())
{
  MagickImage imagePng = new MagickImage("image.png");

  // 'add background'
  MagickImage background = new MagickImage(imagePng.BackgroundColor, imagePng.Width, imagePng.Height);
  images.Add(background);

  // image.png
  images.Add(imagePng);

  // +flatten
  using (MagickImage result = images.Flatten())
  {
    // noTransparency.png
    result.Write("noTransparency.png");
  }
}

But you can also write it like this:

using (MagickImage imagePng = new MagickImage("image.png"))
{
  imagePng.ColorAlpha(imagePngg.BackgroundColor);
  result.Write("noTransparency.png");
}

And your other example cannot be translated with the current release (7.0.0.0016) because +transparent is not yet supported.

convert original.png +transparent "#E6B15A" singleColor.png1

In the next version you will be able to do this:

using (MagickImage original = new MagickImage("original.png"))
{
  original.InverseTransparent(new MagickColor("#E6B15A"));
  original.Write("singleColor.png");
}

But you could do it like this for now:

using (MagickImage original = new MagickImage("original.png"))
{
  original.InverseOpaque(new MagickColor("#E6B15A"), MagickColor.Transparent);
  original.Write("singleColor.png");
}

And you can always ask for more help here: https://magick.codeplex.com/discussions.

like image 193
dlemstra Avatar answered Oct 13 '22 12:10

dlemstra