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

Removing white from image with white background where gradients are present

Aug 27, 2016 at 5:01 PM
Edited Aug 27, 2016 at 5:21 PM
I'm working with images that were created as monochrome TIFFs (colour separations) where I need to convert them to PNGs with a transparent background, removing all the white.

The problem is, the images have gradients in them. So for example I need to be able to replace not just solid white with transparent (which I managed to do already) but also take into account percentages; so for example what was 40% black before would become 100% black but 40% opaque, if that makes sense.

So an example of the source image:
https://www.dropbox.com/s/zy5by7duftpj0gi/Gradient.tif?dl=0

Desired result:
https://www.dropbox.com/s/55hf82swzg3mjk5/Gradient-01.png?dl=0

Looking for recommendations as to how this could be achieved with Magick.NET?

Thanks in advance.
Aug 27, 2016 at 7:29 PM
I saw that you also asked your question here: https://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=30369. Do you need help translating this to Magick.NET?
Aug 27, 2016 at 8:39 PM
Thanks, yes please that would be great.

So for reference the IM command that was provided on that forum:
convert {input file path} -profile sRGB.icc +strip -negate -alpha copy -fill black -colorize 100 {Output file path}

Regards
David
Aug 27, 2016 at 9:08 PM
Edited Aug 27, 2016 at 9:09 PM
That command would translate to this:
using (MagickImage image = new MagickImage("{input file path}"))
{
  image.AddProfile(ColorProfile.SRGB);
  image.Strip();
  image.Negate();
  image.Alpha(AlphaOption.Copy);
  image.Colorize(MagickColors.Black, new Percentage(100));
  image.Write("{Output file path}");
}
I am getting a slightly different result than your desired result though.
Aug 27, 2016 at 9:34 PM
Hi, thank you very much for the help - it does seem to produce the same result as the plain IM command. What do you think is working differently? I did notice that some of the areas that were meant to be nearly solid were still showing some transparency, but I am going to do some more tests as this may have just been how my sample PDFs were set up.

Thanks
David
Aug 27, 2016 at 10:48 PM
Its the same as the IM command. But it looks a bit different from your desired result in your first post. Just wanted to check if this was the result that you were looking for.
Aug 27, 2016 at 11:42 PM
OK thanks. There was one more thing I was struggling with if you are able to help:

I am trying now to adjust the colour of the fill that is applied, so instead of using black, I can define a custom colour using a ushort array within a list called RGBColValues:
                        image.Colorize(new MagickColor(RGBcolValues[k][0], RGBcolValues[k][1], RGBcolValues[k][2]), new Percentage(100));
However, it's always still giving me black within the image. Is there something that I'm missing?
Aug 28, 2016 at 8:48 AM
Are you sure you are passing in the correct values? image.Colorize(MagickColors.Fuchsia, new Percentage(100)); is giving me a correct result. Are you aware that you need to use the full range of the ushort? So 0-65535 instead of a 0-255.
Aug 28, 2016 at 9:16 AM
Sorry, you are exactly right. I was trying to pass it values 0-255 not the full ushort range. Amended and works perfectly. Thank you!!