CMYK Color Conversion

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
pti
Posts: 4
Joined: 2016-08-13T09:35:10-07:00
Authentication code: 1151

CMYK Color Conversion

Post by pti »

I have a PNG image generated by a QR code generator that I want to convert to CMYK and change the color.

Code: Select all

       
        using (MagickImage image = new MagickImage(inputFile))
        {
            image.Format = MagickFormat.Jpg;

            image.AddProfile(ColorProfile.SRGB);
            image.TransformColorSpace(ColorProfile.SRGB, ColorProfile.USWebCoatedSWOP);

            MagickColor srcColor = new ColorCMYK(74, 68, 66, 90);
            MagickColor destColor = new ColorCMYK(100, 0, 0, 0);
            
            image.ColorFuzz = new Percentage(30);
            image.Opaque(srcColor, destColor);

            // Save image 
            image.Write(outputFile);
        }
        
Photoshop tells me the RGB's black is now 74, 68, 66, 90 CMYK so I try to convert to something like 100% cyan but the code gives me CMYK of 0, 100, 100, 0. Changing the new color's CMYK values gives me completely different colors in Photoshop.
What am I doing wrong?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CMYK Color Conversion

Post by fmw42 »

This is just a guess since I do not use any API, only the command line. But you have not changed the colorspace of the image except via profiles, so your image is still using RGB colors as pixels and IM converts your CMYK colors back to RGB. Then lets the profiles do the conversion for display. Try changing the colorspace of the data to CMYK.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: CMYK Color Conversion

Post by snibgo »

I don't use Magick.net nor CMYK so I'm also guessing. Are you mixing units? Eg Photoshop has a scale 0 to 100 but IM has 0 to 255?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CMYK Color Conversion

Post by fmw42 »

Just another additional guess, but your input colorspace is sRGB since PNG does not support CMYK. So why specify the input color in CMYK. Perhaps specify it as RGB. cyan is rgb(0,255,255) or rgb(0,100%,100%).

Snibgo has a good point about color ranges (units) as raw values 0-255 vs percent 0-100.
pti
Posts: 4
Joined: 2016-08-13T09:35:10-07:00
Authentication code: 1151

Re: CMYK Color Conversion

Post by pti »

Iv'e tried

Code: Select all

image.TransformColorSpace(ColorProfile.SRGB, ColorProfile.USWebCoatedSWOP);
image.ColorSpace = ColorSpace.CMYK;
These commands should have changed the colorspace, and Photoshop shows the colorspace of the file as CMYK/8.

If I use

Code: Select all

MagickColor destColor = new ColorCMYK(255, 0, 0, 0);
I still don't get a Cyan only color. It comes out red so I think there is something going on where colors are still being interpreted as RGB instead of CMYK but I don't know what.

I need the image to be "print-ready" using CMYK. Printers don't like RGB. RGB is for screen display only.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: CMYK Color Conversion

Post by snibgo »

What changes do you need? Can you make them in sRGB, then convert to CMYK?

Do you have to do it in a program? Can you use the command line?
snibgo's IM pages: im.snibgo.com
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: CMYK Color Conversion

Post by dlemstra »

ColorCMYK uses a value in the QuantumRange (0-Quantum.Max) and not a percentage. You will need to to use a value in that range.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
pti
Posts: 4
Joined: 2016-08-13T09:35:10-07:00
Authentication code: 1151

Re: CMYK Color Conversion

Post by pti »

I cannot make the change in RGB because the color space is much smaller than CMYK.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CMYK Color Conversion

Post by fmw42 »

dlemstra just told you how to use ColorCMYK. The range of values you need to use depend upon your IM Q level for your compile. Standard compile is Q16 which means you need to use values in the range 0-65535. So just scale percent value by 65535/100 or scale values in the range 0-255 by 65536/255.

See convert -version for you Q level
pti
Posts: 4
Joined: 2016-08-13T09:35:10-07:00
Authentication code: 1151

Re: CMYK Color Conversion

Post by pti »

Thanks for all the suggestions but there might be something wrong with the Magick.Net implementation.
Even when using Quantum values, if I use ColorCMYK(255, 0, 0, 0) my color comes out red. If I use ColorCMYK(0, 255, 0, 0) my color comes out green. So ColorCMYK() seems to be setting RGB values behind the scene. I've even tried this on files I know are CMYK already.

I haven't tried the command line version but when I get around to it, I'll report back.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CMYK Color Conversion

Post by fmw42 »

pti wrote:Thanks for all the suggestions but there might be something wrong with the Magick.Net implementation.
Even when using Quantum values, if I use ColorCMYK(255, 0, 0, 0) my color comes out red. If I use ColorCMYK(0, 255, 0, 0) my color comes out green. So ColorCMYK() seems to be setting RGB values behind the scene. I've even tried this on files I know are CMYK already.

I haven't tried the command line version but when I get around to it, I'll report back.

Are you on Q8 IM compile? If not and for example are on Q16 compile, then quantumrange is 0-65535. Check your Q level from

Code: Select all

convert -version
Post Reply