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

Black and White and Sepia

Feb 6, 2015 at 4:44 PM
Hope someone can help. I am trying to create a two functions for an application so we no longer have to do this in Photoshop.

One of the functions will convert images to black and white while the other will convert images to a sepia tone.

While I can easily convert any image to Grayscale using something like

image.ColorSpace = ColorSpace.GRAY;

the images seem to loose a lot of depth in various grays which we would need to preserve. Preferably I want to simply desaturate the colors of the image in a similar fashion like Photoshop which is what we do but I am trying to eliminate this.

As for converting an image to sepia, I am just plain lost how to best accomplish this. I did look at the the examples here and they make perfect sense
http://www.imagemagick.org/Usage/color_mods/#sepia-tone

I am just now sure how I would implement this within my Magick.Net code. Any quick examples would be appreciated.
Coordinator
Feb 6, 2015 at 6:10 PM
Edited Feb 6, 2015 at 6:37 PM
You can make a image black and white with various methods, one is setting the ColorType to BiLevel:
using (MagickImage logo = new MagickImage("logo:"))
{
  logo.ColorType = ColorType.Bilevel;
  logo.Write("logo.png");
}
You can create a sepia-tone image with the SepiaTone method of MagickImage:
using (MagickImage logo = new MagickImage("logo:"))
{
  logo.SepiaTone(65);
  logo.Write("logo.png");
}
Feb 7, 2015 at 3:56 PM
Not sure how I missed the SepiaTone method. That works great.

As for the black and white, still running into the same problem with the lack of depth (I think because it is turning it into grayscale) plus the Bilevel method adds noise to the image. I think I am trying to emulate more of a "desaturate" colors method.

While it might not be ideal for every image the application would process that calls for black and white, now that I think on it, I might want to give the users a choice since the desaturation seems to preserver midtones better while the grayscale option shows details in the darks better.

The users are photographers so they can be very picky when it comes to black and whites.

We use black and white methods on our website with regular c#. under System.Drawing.Imaging.ColorMatrix but I am not sure how to do this with ImageMagick.
Any other tips on black and whites?
Feb 7, 2015 at 4:35 PM
Just for anyone interested, one other method that does seem promising is

image.Modulate(90, 0, 100);

It seems to allow better control then simply turning it to Grayscale.
Coordinator
Feb 7, 2015 at 5:29 PM
Edited Feb 9, 2015 at 5:27 PM
Just to be clear do you want a black and white image or a grayscale image? For me a black and white image only contains two colors (black and white). You should only use bilevel if that is your goal. But I think you want a grayscale image.

One trick would be adjusting the gamma of the image:
using (MagickImage image = new MagickImage("input.jpg"))
{
  image.ColorType = ColorType.Grayscale;
  image.Gamma(2.0);
  image.Write("output.jpg");
}
But I think you are looking for the ColorMatrix method:
using (MagickImage image = new MagickImage("input.jpg"))
{
  ColorMatrix matrix = new ColorMatrix(3);
  // SetRow won't be available until the next release.
  // Use SetValue(x,y,value) for now (I feel your pain...)
  matrix.SetRow(0, 0.2, 0.5, 0.3);
  matrix.SetRow(1, 0.2, 0.5, 0.3);
  matrix.SetRow(2, 0.2, 0.5, 0.3);
  image.ColorMatrix(matrix);
  image.Write("output.jpg");
}
Feb 9, 2015 at 5:11 PM
Sorry. I should have been clearer. Yes. When I said black and white I was referring to Grayscale. We produce prints for fine art photographers and I learned a while back that some become indignant when saying "Grayscale". Some can be funny about terminology sometimes.

The ColorMatrix option generates a number of arguements error so I may need to stick with the Grayscale option for now or Modulate method.