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

Wrong colors when converting .pam images #7548

Open
user1823 opened this issue Aug 12, 2024 · 17 comments
Open

Wrong colors when converting .pam images #7548

user1823 opened this issue Aug 12, 2024 · 17 comments

Comments

@user1823
Copy link

ImageMagick version

ImageMagick 7.1.1-36 Q16-HDRI x64 852a4e9:20240727

Operating system

Windows

Operating system, version and so on

Windows 11 Build No. 10.0.22631-SP0

Description

When converting a .pam image to .jpg or .png using ImageMagick, the colors in the jpg and png are different.

The .pam image was extracted from a PDF file by using mutool extract. I don't know how to open .pam image files but the colors in the jpg and png files are different from the PDF and also different from each other.

PDF (as displayed in mupdf-gl):
image

JPG (as displayed in IrfanView & Microsoft Photos):
image

PNG (as displayed in IrfanView & Microsoft Photos):
image

Steps to Reproduce

  1. magick test.pam test.png
  2. magick test.pam test.jpg

Images

test.zip

This zip file contains the input image (test.pam) as well as the two output images (test.png and test.jpg).

@fmw42
Copy link

fmw42 commented Aug 12, 2024

Your PAM image is CMYK. PNG does not support that. It only uses RGB. So convert your image before saving using the generic -colorspace sRGB. You will get different results depending how you convert as below using the generic approach or using profiles.

magick test.pam -colorspace sRGB test1.png

test1

Or use profiles. Your image does not have any assigned profile, so I add the best one that I know for CMYK for the input and then a sRGB one for the output. There are many CMYK and many RGB profiles that you can use to tune the result.

magick test.pam -profile /volumes/Fred_Backup/Images/profiles/USWebCoatedSWOP.icc -profile /volumes/Fred_Backup/Images/profiles/sRGB.icc test2.png

test2

Some tools may convert it automatically.

@user1823
Copy link
Author

Thanks for the reply, Fred.

The following command gave me the desired output (where desired means one that matches mupdf-gl):
magick test.pam -profile USWebCoatedSWOP.icc -profile sRGB-v4.icc test1.png

I obtained the profiles from

According to you, does ImageMagick require any changes so that it can handle such conversions better?

(I don't know anything about colorspace or color profiles.)

@fmw42
Copy link

fmw42 commented Aug 12, 2024

Sorry, I do not know what you mean by "does ImageMagick require any changes so that it can handle such conversions better?"

The profile method is the best to use. If your input image has a CMYK profile, you do not need the -profile USWebCoatedSWOP.icc unless you want to convert to that input profile.

@user1823
Copy link
Author

Sorry for not being clear. I wanted to ask whether ImageMagick can be "patched" so that it can automatically handle the conversion (with the correct colors) when I use magick test.pam test.png without needing to specify the profiles.

If no change needs to be made to the ImageMagick codebase, I can close the issue because the problem has been solved for me.

@fmw42
Copy link

fmw42 commented Aug 12, 2024

As far a I know there is no fix or change that can/will be made for that. A developer will have to comment further or someone who know the code better.

It seems that IM uses -colorspace sRGB (or something like it) automatically to do the conversion. It would have no way to know what profiles you want to use and in general leaves these things to the user so as to be as flexible as possible.

Here is my conversion

magick test.pam test.png

test

As you can see it did the conversion, but the results are like using -colorspace sRGB in the command. You can tell it did the conversion as the colors are not negated in the output, which is typical of CMYK input which is interpreted as RGB output.

If you want something automatic, you would have to write a script that detects the PNG output and CMYK input and adds the profile code.

@user1823
Copy link
Author

It would have no way to know what profiles you want to use

Well, I just want the converted image to look as similar to the original image as possible. I don't really have preference for a particular profile.

@fmw42
Copy link

fmw42 commented Aug 13, 2024

There is no automated method currently other than a script. Use the profiles that I suggested.

@snibgo
Copy link

snibgo commented Aug 13, 2024

Well, I just want the converted image to look as similar to the original image as possible.

I understand. But your "original image" is actually an RGB image converted from CMYK, and we don't know how that conversion was done, or whether it was "correct".

I wanted to ask whether ImageMagick can be "patched" so that it can automatically handle the conversion (with the correct colors) when I use magick test.pam test.png without needing to specify the profiles.

IM does automatically handle the conversion, but it doesn't know how to do this correctly. There is no definition of "correct". As @fmw42 says, there is no embedded ICC CMYK colour profile so one possibility is to assign a guessed profile, and convert to some sRGB profile. But this might be the wrong action (eg if the CMYK image was generated by -colorspace CMYK), and either of these profiles might be a bad guess. I think it is better if IM doesn't guess.

@user1823
Copy link
Author

user1823 commented Aug 13, 2024

Do you think that IM should show a warning in this case (when the color profile is not embedded and IM is not confident of the colors used)?

As @fmw42 says, there is no embedded ICC CMYK colour profile so one possibility is to assign a guessed profile
...
and either of these profiles might be a bad guess. I think it is better if IM doesn't guess.

so I add the best one that I know for CMYK for the input

If I understand it correctly, IM is using a guessed profile even now. Otherwise, it won't be able to convert the image, right?

As Fred said, USWebCoatedSWOP.icc is the best for CMYK. So, shouldn't this be the default?

By the way, I still don't understand why the choice between -colorspace sRGB and -profile sRGB-v4.icc changes the color in the output. I mean, the profile of the input file should tell IM what the "correct" colors are and then IM should be able to reproduce them in the output even if no colorspace/profile of the output is specified.

(Sorry if I am being too noob.)

@fmw42
Copy link

fmw42 commented Aug 13, 2024

IM is not using profiles. It uses a direct map probably just -colorspace sRGB.

I said USWebCoatedSWOP.icc was my best guess for CMYK. It works adequately for me. It should not be the default. That should be left to the user.

@snibgo
Copy link

snibgo commented Aug 13, 2024

If I understand it correctly, IM is using a guessed profile even now. Otherwise, it won't be able to convert the image, right?

No. If your command doesn't include -profile, IM doesn't use profiles.

IM's default method for converting between RGB and CMYK uses simple arithmetic. When the colours are normalized to [0,1]:

  Red = 1 - (Cyan*(1-Black) + Black);
  Green = 1 - (Magenta*(1-Black) + Black);
  Blue = 1 - (Yellow*(1-Black) + Black);

The arithmetic used in conversions with -profile is far more complex. This also makes it expensive computationally.

I don't have good data on this, but I suspect the most common ICC CMYK profiles depend on where the image originated: USA (US*.icc), Europe (*FOGRA*.icc), or Japan (Japan*.icc).

@user1823
Copy link
Author

user1823 commented Aug 13, 2024

Thanks for your reply. It makes things clearer to me.

IM's default method for converting between RGB and CMYK uses simple arithmetic.

In my opinion, this makes an even stronger case for adding a warning when the input image doesn't contain a profile.

The warning can read something like "The input image does not have any embedded CMYK colour profile. If the colors in the output image look distorted, try using different color profiles."

By the way, I still don't understand why the choice between -colorspace sRGB and -profile sRGB-v4.icc changes the color in the output. I mean, the profile of the input file should tell IM what the "correct" colors are and then IM should be able to reproduce them in the output even if no colorspace/profile of the output is specified.

This still remains a doubt.

@snibgo
Copy link

snibgo commented Aug 13, 2024

... and then IM should be able to reproduce them in the output even if no colorspace/profile of the output is specified.

But IM doesn't know if you want the output file to be encoded as sRGB or something else.

@user1823
Copy link
Author

Regardless of how the colors are encoded in the output file, they should look the same, right?

@fmw42
Copy link

fmw42 commented Aug 14, 2024

No, different output profiles may look different.

@user1823
Copy link
Author

Ok, it seems that adding a warning is the only thing that IM needs to do.

@fmw42
Copy link

fmw42 commented Aug 14, 2024

Doubt it will happen as the way IM works is to leave things to the user. There is no way to know what the user wants in all cases.

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

3 participants