Color issue when compositing sRGB image over CMYK background and saving as CMYK

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
marcusforsberg
Posts: 3
Joined: 2019-10-08T00:51:21-07:00
Authentication code: 1152

Color issue when compositing sRGB image over CMYK background and saving as CMYK

Post by marcusforsberg »

Hi,

I've been tearing my hair out over this for the passed few days.

I have a project where I need to take a PNG that is in sRGB with a transparent background, and composite it repeatadly in a certain pattern over a background JPG. The background is in CMYK. The final output file is supposed to be in CMYK as well, with the profile stripped away. This image will then be printed, thus needs to be in CMYK.

I have everything working perfectly in terms of the actual sizing, placement, etc, but I'm having issues with color profiles. The colors in the final output file are not showing up correctly in Photoshop when inspecting it. The background itself is visually identical to the input background file, but the overlay PNG has its colors completely distorted.

Here are some sample files illustrating the problem:

https://www.dropbox.com/s/s4x36ofh5lv0v ... y.png?dl=0 - The overlay PNG in sRGB
https://www.dropbox.com/s/qoxwluv5f7g5s ... g.jpg?dl=0 - The background JPG in CMYK
https://www.dropbox.com/s/accd3hliazlkh ... t.jpg?dl=0 - The output file. Notice the colors of the lego figure are wrong.
https://www.dropbox.com/s/uhas6b1dkdst2 ... p.jpg?dl=0 - The same thing but manually created in Photoshop. Notice the colors of both layers match the input.

This is the sample command I am running (the actual command uses different sizes and repeats the image a lot more times, but this shortend sample illustrates the problem with the colors):

Code: Select all

magick -size 1024x1024 canvas:#ffffff \
  \( 'sample-bg.jpg' -negate \) -composite \
  sample-overlay.png -geometry +50+11 -composite \
  sample-overlay.png -geometry +420+450 -composite \
  -quality 100 \
  -colorspace cmyk \
  result.jpg
(I for some reason need to -negate the background when it's in CMYK. I'm compositing the BG over an empty canvas since in some cases the BG might be bigger than expected and need to be cropped on the canvas).

I'm seeing the same result on MacOS, Windows and Linux. Running version 7.0.8-68 Q16 x86_64 2019-10-07.

This really isn't my strongest suite and after hours of googling, looking over these forums, StackOverflow, etc, I'm definitely stuck. I've tried using -colorspace, -set colorspace, and -profiles using different ICC files, and all sorts of combinations of those paramters, but for the life of me I can't get it to treat the colors as expected.

Any pointers in the right direction would be greatly appreciated! Thanks! 😊
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color issue when compositing sRGB image over CMYK background and saving as CMYK

Post by snibgo »

IM will happily composite images that are in different colorspaces, but this is usually not a good idea. You need to convert all inputs to the same colorspace before compositing them. In your case, I suggest converting to sRGB, then composite, then convert the result to CMYK.

sample-bg.jpg has an embedded profile, so use "-profile" for the colorspace conversions.

Windows BAT syntax. For bash, change line-ends to "\" and escape parentheses \( and \).

If sRGB.icc isn't in your current directory, give the full path.

The two commands can be combined into one.

Code: Select all

magick sample-bg.jpg tmp.icc

magick ^
  -size 1024x1024 canvas:#ffffff ^
  ( sample-bg.jpg -profile sRGB.icc ) ^
  -composite ^
  sample-overlay.png -geometry +50+11 -composite ^
  sample-overlay.png -geometry +420+450 -composite ^
  -profile tmp.icc ^
  result.jpg
snibgo's IM pages: im.snibgo.com
marcusforsberg
Posts: 3
Joined: 2019-10-08T00:51:21-07:00
Authentication code: 1152

Re: Color issue when compositing sRGB image over CMYK background and saving as CMYK

Post by marcusforsberg »

Thanks for your help!

Unless I'm misunderstanding something, that command seems to produce an RGB image, at least according to Photoshop. On that image, the colors look correct. How would I go about actually getting result.jpg to be in CMYK? I added "-colorspace cmyk" to the end of the command you provided, but ended up with the exact same result as before (with the colors distorted).

Tested on the version mentioned above, on MacOS.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color issue when compositing sRGB image over CMYK background and saving as CMYK

Post by snibgo »

marcusforsberg wrote:Unless I'm misunderstanding something, that command seems to produce an RGB image, ...
Sorry, my fault, I forgot a critical line, "-profile sRGB.icc" near the end. This is needed because the first image, canvas:#fff, doesn't have an embedded profile. So we have:

Code: Select all

magick sample-bg.jpg tmp.icc

magick ^
  -size 1024x1024 canvas:#ffffff ^
  ( sample-bg.jpg -profile sRGB.icc ) ^
  -composite ^
  sample-overlay.png -geometry +50+11 -composite ^
  sample-overlay.png -geometry +420+450 -composite ^
  -profile sRGB.icc ^
  -profile tmp.icc ^
  result.jpg
snibgo's IM pages: im.snibgo.com
marcusforsberg
Posts: 3
Joined: 2019-10-08T00:51:21-07:00
Authentication code: 1152

Re: Color issue when compositing sRGB image over CMYK background and saving as CMYK

Post by marcusforsberg »

You, sir, are a legend! That seems to work flawlessly! :D Many thanks!

One thing I noticed that isn't *really* an issue for this project, is that the background color seems to darken ever so slightly on the final result. It's just a shade or so darker with my sample background and with a couple of other background images. Again, this isn't really an issue in this case, but if you know of a way to remedy that or have any idea of why it might be, I'd be interested to hear. :)

See here for a pink sample: https://www.dropbox.com/s/aw1fib2i2pwl4 ... e.jpg?dl=1 It's a very subtle difference (original shade to the right, resulting shade to the left). I'm seeing the same thing with the red sample background from my original post.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color issue when compositing sRGB image over CMYK background and saving as CMYK

Post by snibgo »

CMYK and sRGB have different gamuts, meaning some colours in one colourspace have no unique corresponding colour in the other colorspace. This means that some colours (especially saturated colours) will change when they undergo the roundtrip CMYK->sRGB->CMYK. The sample-bg.jpg image undergoes that roundtrip.

We can avoid that roundtrip by doing the work in CMYK instead of sRGB. So we need to convert sample-overlay.png to CMYK. Untested script:

Code: Select all

magick sample-bg.jpg tmp.icc

magick ^
  -size 1024x1024 canvas:#ffffff ^
  -profile sRGB.icc -profile tmp.icc ^
  sample-bg.jpg ^
  -composite ^
  ( sample-overlay.png -profile sRGB.icc -profile tmp.icc ) -geometry +50+11 -composite ^
  ( sample-overlay.png -profile sRGB.icc -profile tmp.icc ) -geometry +420+450 -composite ^
  result.jpg
This command has "-profile sRGB.icc -profile tmp.icc". The first "-profile" assigns the sRGB profile. The second converts to the new profile.

This may, or may not, cure the problem of the slight colour change.

Reading and converting sample-overlay.png twice is inefficient. We can save the first result in memory ("mpr:") and simply use that:

Code: Select all

magick sample-bg.jpg tmp.icc

magick ^
  -size 1024x1024 canvas:#ffffff ^
  -profile sRGB.icc -profile tmp.icc ^
  sample-bg.jpg ^
  -composite ^
  ( sample-overlay.png -profile sRGB.icc -profile tmp.icc +write mpr:OVERL ) -geometry +50+11 -composite ^
  mpr:OVERL -geometry +420+450 -composite ^
  result.jpg
snibgo's IM pages: im.snibgo.com
Post Reply