Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colors inverted on CMYK tiff file conversions #6094

Open
bvb-sc opened this issue Feb 21, 2023 · 17 comments
Open

Colors inverted on CMYK tiff file conversions #6094

bvb-sc opened this issue Feb 21, 2023 · 17 comments

Comments

@bvb-sc
Copy link

bvb-sc commented Feb 21, 2023

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.:

image

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

sample.zip

@fmw42
Copy link

fmw42 commented Feb 21, 2023

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.

@snibgo
Copy link

snibgo commented Feb 21, 2023

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

@bvb-sc
Copy link
Author

bvb-sc commented Feb 22, 2023

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

image

Should it not show correct colors in these cases?

@snibgo
Copy link

snibgo commented Feb 22, 2023

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".

@bvb-sc
Copy link
Author

bvb-sc commented Feb 22, 2023

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:

sample
sample_iccicm_removal
sample_srgb_colorspace
sample_srgb_profile
sample_stripped

@snibgo
Copy link

snibgo commented Feb 22, 2023

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.

@bvb-sc
Copy link
Author

bvb-sc commented Feb 22, 2023

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:
sample_6 3 3-0_Q16

6.7.1-6:
sample_6 7 1-6_Q16

6.9.0-7:
sample_6 9 0-7

6.9.2-7:
sample_6 9 2-7

Earliest version I still have where it's broken:

6.9.12-34:
sample_6 9 12-34

It looks more like something changed along the way, rather than the file itself

@snibgo
Copy link

snibgo commented Feb 23, 2023

Thanks for narrowing the apparent change. A developer may comment.

@urban-warrior
Copy link
Member

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

@bvb-sc
Copy link
Author

bvb-sc commented Feb 24, 2023

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

sample_stripped_cmyk

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

sample_cmyk_negated

In the older versions, there is no need for -negate for both RGB and CMYK color spaces to work

@urban-warrior
Copy link
Member

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).

@fmw42
Copy link

fmw42 commented Feb 24, 2023

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.

@fmw42
Copy link

fmw42 commented Feb 25, 2023

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

sample

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)

@bvb-sc
Copy link
Author

bvb-sc commented Feb 28, 2023

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:
cmyk_japan

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

@fmw42
Copy link

fmw42 commented Feb 28, 2023

There is something wrong with your zip file. I cannot unzip it. Can you try posting it again.

@bvb-sc
Copy link
Author

bvb-sc commented Mar 1, 2023

Hi, I've made a new zip:
cmyk_japan.zip

@fmw42
Copy link

fmw42 commented Mar 1, 2023

This works fine for me:

magick cmyk_japan.tif cmyk_japan.jpg

cmyk_japan

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants