This project has moved and is read-only. For the latest updates, please go here.

CMYK to RGB for JPEG

Apr 10, 2014 at 2:16 PM
I have looked at several threads here regarding CMYK to RGB conversion. I've also modeled my code after the samples. Neither of these routines places an icc profile in the jpeg file. I'm using version 6.8.8.901 x86.
    //Attempt 1
    using (MagickImage image = new MagickImage(args[0]))
    {
        image.AddProfile(ColorProfile.SRGB);
        image.ColorSpace = ColorSpace.sRGB;
        image.Write("cmyk1.jpg");
    }

    //Attempt 2
    using (MagickImage image = new MagickImage(args[0]))
    {
        image.AddProfile(new ColorProfile("sRGB_v4_ICC_preference.icc"));
        image.ColorSpace = ColorSpace.sRGB;
        image.Write("cmyk2.jpg");
    }
Please help me understand what I'm doing wrong.

Thanks,
Darren
Apr 10, 2014 at 2:43 PM
Do you want to perform a CMYK to RGB conversion or do you want to add your profile to the output image? Does your source image contain a color profile? Can you add a link to your input image?
Apr 10, 2014 at 5:52 PM
I want to convert CMYK to RGB and embed an icc profile so the colors match. I don't believe my source image has a color profile. Image Here's the direct link https://www.dropbox.com/s/1xrowit0719d47g/00000011.jpg


This is what identify.exe returns on it.
D:\Apps\ImageMagick-6.8.8-Q16>identify -verbose 00000003.jpg
Image: 00000003.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 675x286+0+0
  Resolution: 179x179
  Print size: 3.77095x1.59777
  Units: PixelsPerInch
  Type: ColorSeparation
  Endianess: Undefined
  Colorspace: CMYK
  Depth: 8-bit
  Channel depth:
    cyan: 8-bit
    magenta: 8-bit
    yellow: 8-bit
    black: 8-bit
  Channel statistics:
    Cyan:
      min: 0 (0)
      max: 255 (1)
      mean: 44.982 (0.1764)
      standard deviation: 54.7177 (0.214579)
      kurtosis: 1.22171
      skewness: 1.306
    Magenta:
      min: 0 (0)
      max: 255 (1)
      mean: 63.3404 (0.248394)
      standard deviation: 73.9775 (0.290108)
      kurtosis: 0.786581
      skewness: 1.296
    Yellow:
      min: 0 (0)
      max: 255 (1)
      mean: 57.5256 (0.22559)
      standard deviation: 77.7452 (0.304883)
      kurtosis: 0.980444
      skewness: 1.50539
    Black:
      min: 0 (0)
      max: 255 (1)
      mean: 26.7778 (0.105011)
      standard deviation: 58.3135 (0.22868)
      kurtosis: 7.24378
      skewness: 2.83538
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 48.1564 (0.188849)
      standard deviation: 66.9169 (0.262419)
      kurtosis: 2.52627
      skewness: 1.81497
  Total ink density: 330.98%
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: cmyk(255,255,255,0)
  Border color: cmyk(223,223,223,0)
  Matte color: cmyk(189,189,189,0)
  Transparent color: cmyk(0,0,0,0)
  Interlace: JPEG
  Intensity: Undefined
  Compose: Over
  Page geometry: 675x286+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 93
  Orientation: TopLeft
  Properties:
    date:create: 2014-04-09T13:15:52-05:00
    date:modify: 2014-04-02T16:06:23-05:00
    exif:BitsPerSample: 8, 8, 8, 8
    exif:ColorSpace: 65535
    exif:Compression: 6
    exif:DateTime: 2014:03:24 14:37:47
    exif:ExifImageLength: 286
    exif:ExifImageWidth: 675
    exif:ExifOffset: 232
    exif:ExifVersion: 48, 50, 50, 49
    exif:ImageLength: 1074
    exif:ImageWidth: 2531
    exif:JPEGInterchangeFormat: 382
    exif:JPEGInterchangeFormatLength: 5027
    exif:Orientation: 1
    exif:PhotometricInterpretation: 5
    exif:ResolutionUnit: 2
    exif:SamplesPerPixel: 4
    exif:Software: Adobe Photoshop CS6 (Windows)
    exif:XResolution: 1790000/10000
    exif:YResolution: 1790000/10000
    jpeg:colorspace: 4
    jpeg:sampling-factor: 1x1,1x1,1x1,1x1
    signature: 2c16abe5ce37547da68a619f286b31eb4e186c1cff36ec35e5369918e11d374b
  Profiles:
    Profile-8bim: 7494 bytes
    Profile-exif: 5415 bytes
    Profile-iptc: 15 bytes
      City[1,90]: 0x00000000: 254700                                        -%
      unknown[2,0]:
    Profile-xmp: 3222 bytes
  Artifacts:
    filename: 00000003.jpg
    verbose: true
  Tainted: False
  Filesize: 125KB
  Number pixels: 193K
  Pixels per second: 32.18MB
  User time: 0.016u
  Elapsed time: 0:01.005
  Version: ImageMagick 6.8.8-10 Q16 x64 2014-03-25 http://www.imagemagick.org
Thanks,
Darren
Apr 10, 2014 at 7:38 PM
There will be no color conversion if the original image does not contain an image profile. I have updated the documentation to explain this better. You should do the following:
using (MagickImage image = new MagickImage("meat.jpg"))
{
  image.AddProfile(ColorProfile.USWebCoatedSWOP); // Or another CMYK profile
  image.AddProfile(ColorProfile.SRGB);
  image.ColorSpace = ColorSpace.sRGB;
  image.Write("meat.rgb.jpg");
}
Apr 11, 2014 at 2:03 PM
Thanks Dirk. I will try that to see if it works. It's just a bit confusing because ImageMagick/GraphicsMagick doesn't require an image to have a profile to correct the color using the convert command. I just do convert <file.jpg> -profile sRGB.icc <outfile.jpg> and it produces the expected result. Maybe under the hood convert.exe is doing what you propose.

Thanks,
Darren
Apr 11, 2014 at 2:15 PM
The colors are closer but they do not match the original. I assume that's because it's mapping USWebCoatedSWOP to SRGB. I'm not sure what convert.exe is doing it but it is working perfectly. Is it possible to have an implementation in your code that works like convert.exe?
Apr 11, 2014 at 2:36 PM
Can you also add a link to the result you are getting from convert? I am not getting the correct result on the command line when I don't specify the CMYK profile.
Apr 11, 2014 at 2:59 PM
Apr 11, 2014 at 3:41 PM
Edited Apr 11, 2014 at 3:41 PM
ImageMagick and GraphicsMagick produce different results so you should really use convert instead of gm.

Your output image is still CMYK and the colors are negated in Windows photo viewer:
C:\Test\jpg>identify i.jpg
i.jpg JPEG 675x286 675x286+0+0 8-bit CMYK 185KB 0.000u 0:00.003
Apr 11, 2014 at 5:44 PM
My mistake. GM converts it properly, IM does not as you have shown however the colors are fine when opened with a program that respects the color profile such as Windows Paint in Windows 8.1. I guess I'm stuck now that IM doesn't translate the colors properly and IM.Net works just like IM. Thanks for your help. Sorry for the run-around.

BTW - Below is GM's convert, which is the image linked a couple threads above.
D:\Apps\GraphicsMagick-1.3.19-Q16>gm identify -verbose i3.jpg
Image: i3.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Geometry: 675x286
  Class: DirectClass
  Type: true color
  Depth: 8 bits-per-pixel component
  Channel Depths:
    Red:      8 bits
    Green:    8 bits
    Blue:     8 bits
  Channel Statistics:
    Red:
      Minimum:                     0.00 (0.0000)
      Maximum:                 65535.00 (1.0000)
      Mean:                    47978.79 (0.7321)
      Standard Deviation:      18462.24 (0.2817)
    Green:
      Minimum:                     0.00 (0.0000)
      Maximum:                 65535.00 (1.0000)
      Mean:                    43794.90 (0.6683)
      Standard Deviation:      21085.24 (0.3217)
    Blue:
      Minimum:                     0.00 (0.0000)
      Maximum:                 65535.00 (1.0000)
      Mean:                    45090.56 (0.6880)
      Standard Deviation:      21685.88 (0.3309)
  Resolution: 72x72 pixels/inch
  Filesize: 54.8K
  Interlace: No
  Orientation: TopLeft
  Background Color: white
  Border Color: #DFDFDF
  Matte Color: #BDBDBD
  Page geometry: 675x286+0+0
  Compose: Over
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  JPEG-Quality: 75
  JPEG-Colorspace: 2
  JPEG-Colorspace-Name: RGB
  JPEG-Sampling-factors: 2x2,1x1,1x1
  Signature: 2c6e584c92d1a9fa4fa30b814624500c27f9e89258db86a1ba50930aa9d0b75c
  Profile-iptc: 7494 bytes
    City:
  0x00000000: 254700                                        -%
    unknown:

    unknown:
  _`áÅA?,aÿ
  Profile-EXIF: 5415 bytes
    Image Width: 2531
    Image Length: 1074
    Bits Per Sample: 8
    Photometric Interpretation: 5
    Orientation: 1
    Samples Per Pixel: 4
    X Resolution: 1790000/10000
    Y Resolution: 1790000/10000
    Resolution Unit: 2
    Software: Adobe Photoshop CS6 (Windows)
    Date Time: 2014:03:24 14:37:47
    Exif Offset: 232
    Exif Version: 0221
    Color Space: 65535
    Exif Image Width: 675
    Exif Image Length: 286
  Profile-XMP: 3222 bytes
  Tainted: False
Apr 12, 2014 at 8:21 AM
Can you add a link to your GM output image? It is strange that identify does not report an embedded icc profile.
Apr 15, 2014 at 1:32 PM
gm convert 00000003.jpg -profile sRGB.icc 3convert.jpg
https://www.dropbox.com/s/3lpj939zkuntxrq/3convert.jpg

gm convert 00000003.jpg -profile sRGB.icc -colorspace RGB 3convertRGB.jpg
https://www.dropbox.com/s/4zcgu8vky7uo59p/3convertRGB.jpg
Apr 15, 2014 at 3:48 PM
I checked the source code of GraphicsMagick and they also only convert the colors when there is a source profile. Your 3convert.jpg image is still CMYK and 3convertRGB.jpg only had its colorspace changed and was not converted. It looks too blue on my monitor. With the following code I come really close to your CMYK source image.
using (MagickImage image = new MagickImage("00000003.jpg"))
{
  image.AddProfile(ColorProfile.CoatedFOGRA39);
  image.AddProfile(ColorProfile.SRGB);
  image.Write("00000003.rgb.jpg");
}
The colors will never be the same but the colors of 00000003.rgb.jpg are closer too 00000003.jpg then your 3convertRGB.jpg image. When you look at the bottom 'Sugerdale' logo the color is too red compared with the original image. And the meat on the right is too purple in the centre.
Apr 18, 2014 at 1:56 PM
Thanks. I agree the colors are closer but they are still off. I'm not looking for perfection but perceptibly correct would be nice. Is there really no way to convert these images to match?
Apr 18, 2014 at 3:32 PM
Edited Apr 18, 2014 at 7:54 PM
You will never exactly match the colors. A bit more information can be found here: http://www.sant-media.co.uk/2010/06/color-management-for-beginners-profiles-explained/

Edit:

I have tried a couple more profiles and it looks like you get a better result with the 'USWebUncoated.icc' CMYK profile. You can download that here: https://www.adobe.com/support/downloads/detail.jsp?ftpID=3680
Oct 21, 2014 at 4:43 PM
Dirk,
I have another question regarding this topic. Please look at this image Cereal https://app.box.com/s/qaqn8sue5zdkhz32scso. Is there any hope of converting this to an image in either RGB or CMYK JPEG or PNG that looks normal? This image opens up in Photoshop and looks great but all the typical viewers I used display this image in funky green colors. I'm guessing that I'm out of luck on this one too because I do not see an embedded profile. Thanks for your help.


Here's the gm.exe identify output.
λ gm identify -verbose 112913_ACM_CIR_GATE_V01_ALL_p1_1.tif
Image: 112913_ACM_CIR_GATE_V01_ALL_p1_1.tif
Format: TIFF (Tagged Image File Format)
Geometry: 271x359
Class: DirectClass
Type: color separated
Depth: 8 bits-per-pixel component
Channel Depths:
Cyan:     8 bits
Magenta:  8 bits
Yellow:   8 bits
Black:    8 bits
Channel Statistics:
Cyan:
  Minimum:                  5911.00 (0.0902)
  Maximum:                 65535.00 (1.0000)
  Mean:                    49718.69 (0.7587)
  Standard Deviation:      15650.08 (0.2388)
Magenta:
  Minimum:                     0.00 (0.0000)
  Maximum:                 63479.00 (0.9686)
  Mean:                    30915.05 (0.4717)
  Standard Deviation:      12021.98 (0.1834)
Yellow:
  Minimum:                 10537.00 (0.1608)
  Maximum:                 63736.00 (0.9725)
  Mean:                    33732.91 (0.5147)
  Standard Deviation:       9256.98 (0.1413)
Black:
  Minimum:                     0.00 (0.0000)
  Maximum:                     0.00 (0.0000)
  Mean:                        0.00 (0.0000)
  Standard Deviation:          0.00 (0.0000)
Resolution: 200x200 pixels/inch
Filesize: 87.8K
Interlace: No
Orientation: Unknown
Background Color: white
Border Color: #DFDFDF
Matte Color: #BDBDBD
Page geometry: 271x359+0+0
Compose: Over
Dispose: Undefined
Iterations: 0
Compression: JPEG
Signature: ff7e4f16022c10de4f35fea4c42b8a55827d983d75ea015e7fe0f75972406043
Tainted: False

Tiffinfo.exe output

λ tiffinfo 112913_ACM_CIR_GATE_V01_ALL_p1_1.tif
TIFF Directory at offset 0x15e50 (89680)
Image Width: 271 Image Length: 359
Resolution: 200, 200 pixels/inch
Bits/Sample: 8
Compression Scheme: JPEG
Photometric Interpretation: separated
Samples/Pixel: 4
Rows/Strip: 359
Planar Configuration: single image plane

Thanks,
Darren
Oct 23, 2014 at 1:42 PM
It looks odd in all the programs I use to view it. Can you show me how it looks in Photoshop?
Oct 23, 2014 at 2:44 PM
Edited Oct 23, 2014 at 3:46 PM
Sure. Here's a link to a screenshot with the image loaded in Photoshop.
https://app.box.com/s/dafje10zsw7iiv8rmiui

It may also work in gimp but i haven't tried it yet.

Update: Nope GIMP doesn't work. It doesn't look like it support CMYK.
Oct 23, 2014 at 11:18 PM
I am not sure if I can fix this since other viewers also show the image 'greenish' but I will try to take a look at it this weekend.
Oct 26, 2014 at 9:57 PM
Edited Oct 27, 2014 at 7:06 AM
I took me a while but I figured out how Photoshop determines the colors. In the metadata of the image there is a part that tells me the pixels are stored as YCCK instead of CMYK. The next version of Magick.NET (7.0.0.0006) will detect this and create an image with the correct colors. I was able to create the following image with your sample image: https://www.dropbox.com/s/ecdaga7mutv1tzj/112913_ACM_CIR_GATE_V01_ALL_p1_1.jpg?dl=0.
Oct 27, 2014 at 8:33 PM
Awesome! The dropbox image looks great. Thanks.
Oct 27, 2014 at 9:41 PM
Edited Oct 27, 2014 at 9:44 PM
Your welcome but you should really watch this Ted talk: http://www.ted.com/talks/jill_shargaa_please_please_people_let_s_put_the_awe_back_in_awesome. What I did does not qualify as awesome :P

p.s. I moved the release of Magick.NET 7.0.0.0006 forward, you can download it now.
Oct 28, 2014 at 1:25 AM
ok, I stand corrected. Your changes were not awesome. Your changes didn't rival the wheel nor did they rival the pyramids at Giza. So let me rephrase. Thanks for doing a slightly above adequate job and letting me know when it was finished. ;)

I'll test it out tomorrow.

Darren
Oct 28, 2014 at 7:27 PM
Edited Oct 28, 2014 at 7:28 PM
Tested it. Works great. Thanks for the non-awesome update.