Getting pixel color

Jul 15, 2013 at 1:38 PM
I have been trying to get color of a pixel, and have used the examples found in the forum. But it seems like using the methods is not giving the right result for the alpha channel.

The files i am having problems with is this file returns 255, where another file returns 0 for the alpha channel

The file that return 0 is not even transparent - is there a way to better determine color and transparency.

This is my code.

Pixel pixel = image.GetReadOnlyPixels().GetPixel(x, y);
red = pixel.GetChannel(0);
green = pixel.GetChannel(1);
blue = pixel.GetChannel(2);
alpha = pixel.GetChannel(3);
Coordinator
Jul 15, 2013 at 3:41 PM
Edited Jul 15, 2013 at 4:18 PM
It seems the transparency colors are inverted. Magick.NET is using the old method (http://www.imagemagick.org/Usage/color_basics/#color_semitrans). I will look into it and see if I can figure out why it is inverted.
Jul 15, 2013 at 6:52 PM
I have implemented the inversion this way:

alpha = 255 - pixel.GetChannel(3);

and it works perfect now :-)

Is there a way to identify what each channel is reprecenting Red/Green/Blue/Alpha/Cyan and so on?
Coordinator
Jul 16, 2013 at 10:31 AM
I did some research and the value is correct for ImageMagick 6. It seems that the inversion will happen in ImageMagick 7. I will keep it this way for now. You can identify each channel by the Format of the image.
using(PixelCollection pixels = image.GetReadOnlyPixels())
{
  // RGBA:
  Pixel pixel = GetPixel(x, y);
  red = pixel.GetChannel(0);
  green = pixel.GetChannel(1);
  blue = pixel.GetChannel(2);
  alpha = pixel.GetChannel(3);

  // CMYK:
  Pixel pixel = GetPixel(x, y);
  cyan = pixel.GetChannel(0);
  magenta = pixel.GetChannel(1);
  yellow = pixel.GetChannel(2);
  key = pixel.GetChannel(3);

  // Gray: (uses 4 channels in IM6 this will change in IM7)
  Pixel pixel = GetPixel(x, y);
  gray = pixel.GetChannel(0);
}