Replace all pixels on a image with an specific color

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
pablobhz
Posts: 69
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Replace all pixels on a image with an specific color

Post by pablobhz »

Hello everyone.

I have an image, that is just an empty square.
The image is a TIFF Grayscale, an negative of an specific CMYK color.

This CMYK color, is composed by (example):
0% C
30% M
20% K
0% Y

I know i can't create an CMYK color on the .NET interface, however, i've found the following calculation:
R = 255 * (1-C) * (1-K)
G = 255 * (1-M) * (1-K)
B = 255 * (1-Y) * (1-K)

This way, i could convert the CMYK color to RGB.

Does MagickNET offers some interface to do this convertion (instead of manually doing all calculations) ?
PS: The values for the CMYK color come from a txt file, so they'll always be there.

Basically what i'm trying to achieve, is an faster way to scan trough all image pixels and replace all pixels that aren't white with this specific color.
Also, i'm worried about one last thing: i want to combine the final image with another existent image, but without losing any of its data.

Can i just use the Combine() method of MagickNET, or do i have to replace the specified pixels (that aren't white pixels) in the destination image with pixels from source image(the one with specific color) ?

Thanks in advance.
pablobhz
Posts: 69
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Replace all pixels on a image with an specific color

Post by pablobhz »

I forgot to post.
This is the current code i have:

Code: Select all

 using (MagickImage spotImage = new MagickImage(@"C:\DI_PLOT\Bitmaps\8FILES_IMPOSED_4UP_BACK.PDF (Imposition)00.tif"))
            {

                float[] cmyk = new float[4];
                cmyk[0] = 0; //C
                cmyk[1] = 100; //M
                cmyk[2] = 100; //Y
                cmyk[3] = 0; //K
                //Converting to RGB
                var r = (int)(255 * (1 - cmyk[0]) * (1 - cmyk[3]));
                var g = (int)(255 * (1 - cmyk[1]) * (1 - cmyk[3]));
                var b = (int)(255 * (1 - cmyk[2]) * (1 - cmyk[3]));



                using (PixelCollection pc = spotImage.GetPixels())
                {
                    int channelsCount = pc.Channels;
                    for (int y = 0; y < spotImage.Height; y++)
                    {
                        var pcv = pc.GetArea(0, y, spotImage.Width, 1);
                        for (int x = 0; x < pcv.Length; x += channelsCount)
                        {

//Here is what i'm missing (i suppose). Based on another post , this is the fatest way to scan trough all image pixels. But how to replace non-white pixels ?
                        }
                    }
                }
            }

I've also tried this approach:

Code: Select all

MagickColor CMYKColor = new ColorCMYK(0, 100, 100, 0);
                int R = CMYKColor.R;
                int G = CMYKColor.B;
                int B = CMYKColor.B;                
But its values are different than manual calculation (manually calculating, i have 100,0,0(RGB). Using MagickNET conversion i got 0,100,100(RGB).
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Replace all pixels on a image with an specific color

Post by dlemstra »

Is 8FILES_IMPOSED_4UP_BACK.PDF (Imposition)00.tif the square single color CMYK image? I also don't understand the goal of your application. Can you explain what you goal is? There might be easier ways if you just want to create a CMYK image with a single color.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
pablobhz
Posts: 69
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Replace all pixels on a image with an specific color

Post by pablobhz »

First step : combine all 4 cmyk images
Second: Add this square cmyk image onto it
This is part of my app. It's a preview of the image that is going to be print (industrial printers , large scale).

EDIT: I believe what i'm need to do is to composite the image. Thats it.
After converting the Imposition image to the CMYK color , i want to composite it over the combined image(made by 4 CMYK negatives).

The final image should look like this:
Image
See the red square ? Thats what i meant. Thats why i believe what i need to do is to composite.
I have the code already built (to composite images), with MagickNET. Its used in the first part of my app:
pablobhz
Posts: 69
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Replace all pixels on a image with an specific color

Post by pablobhz »

My current try:

Code: Select all

           ushort c_CMYK = 0;
            ushort m_CMYK = 100;
            ushort y_CMYK = 100;
            ushort k_CMYK = 0;
            using (MagickImage spotImage = new MagickImage(@"C:\inputfolder\8FILES_IMPOSED_4UP_BACK.PDF (Imposition)00.TIF"))
            {
                spotImage.ColorSpace = ColorSpace.CMYK;
                using (PixelCollection pc = spotImage.GetPixels())
                {
                    foreach(Pixel p in pc)
                    {
                        if (p.GetChannel(0) == 0)
                            p.SetChannel(0, c_CMYK);
                        if (p.GetChannel(1) == 0)
                            p.SetChannel(1, m_CMYK);
                        if (p.GetChannel(2) == 0)
                            p.SetChannel(2, y_CMYK);
                        if (p.GetChannel(3) == 0)
                            p.SetChannel(3, k_CMYK);
                        pc.Set(p);
                    }                         
                }
                    spotImage.Write(@"C:\inputfolder\Bitmaps\spotfill.png");                    
            }                           
            }  
However this produces an image exactly like the original one. Nothing changed.
Post Reply