ImageMagick
ImageMagick copied to clipboard
Colors inverted on CMYK tiff file conversions
ImageMagick version
7.1.0-62
Operating system
Other (enter below)
Operating system, version and so on
Ubuntu Focal 20.04, Windows 10
Description
For some CMYK tiff files colors are inverted when doing a conversion using ImageMagick, e.g.:

Steps to Reproduce
With the attached file, you can run the following command to reproduce the issue:
magick <input_file.tiff> <output_file.png>
Images
PNG does not support CMYK and treats is as if RGB. So the colors would be inverted. If you want to convert to PNG add -colorspace sRGB or use profiles.
As fmw42 says, PNG cannot record CMYK values, so IM will automatically convert pixel values to sRGB. However, the input file sample.tiff contains an embedded ICC CMYK colour profile. So the result PNG file is encoded as sRGB but with a CMYK profile.
This is bad news. Viewing software will either ignore the profile, or will regard the profile and show negative colours.
You should either strip the profile, or (better) convert the image to a sRGB profile before saving it:
magick sample.tiff -profile sRGB.icc out.png
Hi,
True, PNG does not support CMYK, however it's not only for PNG output. Same command(s) for JPG file (which does support CMYK), also results in negated colors (even with stripping /removing icc,icm / converting to sRGB.icc / colorspace):
magick sample.tiff sample.jpg
magick sample.tiff -strip sample_stripped.jpg
magick sample.tiff -profile sRGB.icc sample_srgb_profile.jpg
magick sample.tiff -colorspace sRGB sample_srgb_colorspace.jpg
magick sample.tiff +profile icc,icm sample_iccicm_removal.jpg

Should it not show correct colors in these cases?
You show screengrabs of some program that displays images. What program is that? Perhaps that program has a problem.
JPG can record CMYK values. "-strip" by itself doesn't convert pixel values from CMYK to sRGB. If you want sRGB and use "-strip", then you should also "-colorspace sRGB".
The software in the screenshots is Adobe Photoshop, but same result is shown in Windows Explorer, Windows Photoviewer, Windows Photos, Gimp and IrfanView or when uploading to a hosting site and seeing it through a browser.
Using this command also results in inverted colors:
magick sample.tiff -strip -colorspace sRGB sample.jpg
If it helps, these are the output files from earlier:

There seems to be something weird in that file, sample.tiff. If we copy it with tiffcp:
tiffcp sample.tiff sample2.tiff
... and then repeat your experiments on sample2.tiff, the problem seems to be fixed.
I ran some experiments on older versions of IM, on which colors are not inverted for this file:
convert sample.tiff sample.jpg
6.3.3-0:

6.7.1-6:

6.9.0-7:

6.9.2-7:

Earliest version I still have where it's broken:
6.9.12-34:

It looks more like something changed along the way, rather than the file itself
Thanks for narrowing the apparent change. A developer may comment.
By default, Adobe tools writes inverted CMYK pixels to TIFF/JPEG. Adobe tools allow you to invert the colors before you export the image. Some tools assume CMYK inversion, however, ImageMagick processes raw pixels and leaves it to the user to decide what operations they want to perform on the pixels. For TIFF/JPEG with inverted Adobe CMYK pixels, you can correct with this command:
magick sample.tiff -colorspace sRGB -negate sample.jpg
Aside, here's what the JPEG library says about this issue:
CAUTION: it appears that Adobe Photoshop writes inverted data in CMYK JPEG files: 0 represents 100% ink coverage, rather than 0% ink as you'd expect. This is arguably a bug in Photoshop, but if you need to work with Photoshop CMYK files, you will have to deal with it in your application. We cannot "fix" this in the library by inverting the data during the CMYK<=>YCCK transform, because that would break other applications
The command above indeed works for this particular file and specifically to RGB colorspace, but how would I go about and detect when I need to use -negate or not, can it be obtained via running identify command first?
Also, if I wish to stay within CMYK spectrum, the colors also get negated and using -negate results in a completely black image.
magick sample.tiff -strip -colorspace CMYK sample.jpg

magick sample.tiff -strip -colorspace CMYK -negate sample.jpg

In the older versions, there is no need for -negate for both RGB and CMYK color spaces to work
In an older version we would invert the CMYK values, however, that caused issues with non-Adobe pipelines that expect 100% black to be full coverage, whereas with Adobe, 0% is full black coverage (which makes no sense).
urban-warrior: How does the TIFF file get displayed properly? There must be something in the xmp profile or other tiff info to tell the viewers how to display it properly. Can we not access that information and convert (negate) it when processing or saving to other formats such as JPG?
When I view the Tiff file in Preview and other viewers it looks normal. Only when converted to JPG does it look inverted. So there is something in the TIFF file that knows and tells the viewers how to make the TIFF look correct.
Here is how you can do that. Extract the profile. Use profiles to convert to sRGB. Negate the image. Use the extracted profile to set it back to CMYK.
magick sample.tiff sample.icc
magick sample.tiff -profile /Users/fred/images/profiles/sRGB.icc -negate -profile sample.icc sample.jpg

You can even test if there is a profile by:
magick sample.tiff -format "%[profiles]\n" info:
icc,xmp
So there is an icc profile, which you can find out about by:
magick sample.tiff -format "%[profile:icc]\n" info:
ISO Coated v2 (ECI)
Hi Fred,
Thank you for the suggestion, it works quite nicely for the provided tiff file. The only issue is, that for other files, colors get unnecessarily negated, which is also not desired. E.g. a sample file with Japan Color 2001 Uncoated embedded:
magick cmyk_japan.tif -format "%[profile:icc]" info:
Japan Color 2001 Uncoated
magick cmyk_japan.tif cmyk_japan.icc
magick cmyk_japan.tif -profile sRGB.icc -negate -profile cmyk_japan.icc cmyk_japan.jpg
Has this as output:

Whereas the original is blue cmyk_japan.zip
urban-warrior: How does the TIFF file get displayed properly? There must be something in the xmp profile or other tiff info to tell the viewers how to display it properly. Can we not access that information and convert (negate) it when processing or saving to other formats such as JPG?
When I view the Tiff file in Preview and other viewers it looks normal. Only when converted to JPG does it look inverted. So there is something in the TIFF file that knows and tells the viewers how to make the TIFF look correct.
Would be great if we could find this out
There is something wrong with your zip file. I cannot unzip it. Can you try posting it again.
Hi, I've made a new zip: cmyk_japan.zip
This works fine for me:
magick cmyk_japan.tif cmyk_japan.jpg

It looks to me like some tiffs have inverted CMYK channels and others do not. How were your previous TIFFs created (what tool) and how was this TIFF created (what tool)? Perhaps different tools are creating good TIFFs. Or perhaps they had different TIFF creators at different times after which one was corrected.
Or perhaps there is some flag in the xmp profile or elsewhere in the TIFF meta data.
An IM developer will need to comment.