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

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
jedatkinports
Posts: 5
Joined: 2018-11-15T04:25:57-07:00
Authentication code: 1152

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

Post 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
Last edited by jedatkinports on 2018-11-15T07:06:26-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
jedatkinports
Posts: 5
Joined: 2018-11-15T04:25:57-07:00
Authentication code: 1152

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
jedatkinports
Posts: 5
Joined: 2018-11-15T04:25:57-07:00
Authentication code: 1152

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

Post 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!
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

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

Post by dlemstra »

You could also use `TransformColorSpace` instead of `AddProfile`.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply