Page 1 of 1

Colors are too bright when converting an image from CMYK to sRGB colorspace

Posted: 2018-11-15T05:47:09-07:00
by jedatkinports
Here is my code:

Code: Select all

using (MagickImage image = new MagickImage(sourceImagePath))
{
    var whiteColor = new MagickColor("#ffffff");
    var blackColor = new MagickColor("#000000");
    image.BackgroundColor = blackColor;
    image.ColorAlpha(blackColor);

    image.Format = MagickFormat.Jpg;
    image.Quality = 80;

    image.AddProfile(ColorProfile.USWebCoatedSWOP); // Add a CMYK profile if your image does not contain a color profile.
    image.AddProfile(ColorProfile.SRGB); // Adding the second profile will transform the colorspace from CMYK to RGB

    // This "if statement" was missing. This is now the complete code which I have used to generate the .jpg file.

    if (newWidth < image.Width)
    {
        image.Resize(newWidth, 0);
    }
    
    image.Write(destinationImagePath);
}
I'm trying to to convert the .TIFF (CMYK colorspace) image to .JPEG (sRGB colorspace) format.

https://ufile.io/qv3jt
https://ufile.io/zxyv4

Re: Colors are too bright when converting an image from CMYK to sRGB colorspace

Posted: 2018-11-15T06:58:07-07:00
by snibgo
Your input already has an embedded CMYK profile. Why are you converting it to another?

Viewed in Microsoft Photo Viewer, your sRGB jpeg is lighter than your CMYK tiff. It is also smaller, so you have done more to the image than your code shows.

Converting at the command line, with or without the extra CMYK profile, the result looks fine.

Re: Colors are too bright when converting an image from CMYK to sRGB colorspace

Posted: 2018-11-15T07:08:18-07:00
by jedatkinports
I have added another "if statement" which I had in my code. Now there is the complete code. I have added another profile because without it, the new image came out with inverted (negative) colors. I will try it from the command line now and report back. Could you post the command you have used so that I can reproduce it on my machine? Thank you.

This is the file, which I get if I remove the second CMYK profile: https://ufile.io/zpfi8

SECOND EDIT

It seems that the

Code: Select all

image.Resize(newWidth, 0);
and

Code: Select all

image.ColorAlpha(whiteColor);
are the ones which are causing the problem.

Re: Colors are too bright when converting an image from CMYK to sRGB colorspace

Posted: 2018-11-15T07:40:58-07:00
by snibgo
The commands I used were:

Code: Select all

magick 188-0.tif -profile sRGB.icc x0.tiff

magick 188-0.tif -profile USWebCoatedSWOP.icc -profile sRGB.icc x1.tiff
The unnecessary conversion to USWebCoatedSWOP shouldn't make a noticeable difference, and doesn't.

I don't use Magick.net, and I'm not sure what AddProfile() does. It may not do exactly the same as command-line "-profile", which assigns the profile if the image didn't have an embedded profile, or converts to the new profile if one was already embedded. In both cases, it embeds the new profile.

From your "I have added another profile because without it, the new image came out with inverted (negative) colors", I guess AddProfile always assigns, so either it has a bug or you are using the wrong function.

Re: Colors are too bright when converting an image from CMYK to sRGB colorspace

Posted: 2018-11-15T07:52:41-07:00
by jedatkinports
I have resolved the issue by first converting the image to jpg and do all the processing except resizing. Then I save the file and after that I open it again to resize it. This way it works fine. But if I resize it the same time as I do the rest of the processing the image looks completely different.

The code with which I ended up is:

Code: Select all

using (MagickImage image = new MagickImage(sourceImagePath))
{
     var whiteColor = new MagickColor("#ffffff");
     var blackColor = new MagickColor("#000000");

     image.BackgroundColor = whiteColor;

     image.Format = MagickFormat.Jpg;
     image.Quality = 80;            

     image.Write(destinationImagePath);
}

using (MagickImage image = new MagickImage(destinationImagePath))
{
     if (newWidth < image.Width)
     {
         image.Resize(newWidth, 0);
     }

     image.Write(destinationImagePath);
}
Thank you for all your help!

Re: Colors are too bright when converting an image from CMYK to sRGB colorspace

Posted: 2018-11-15T10:22:59-07:00
by dlemstra
You could also use `TransformColorSpace` instead of `AddProfile`.