Wrong colors when converting .pam images
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):
JPG (as displayed in IrfanView & Microsoft Photos):
PNG (as displayed in IrfanView & Microsoft Photos):
Steps to Reproduce
-
magick test.pam test.png -
magick test.pam test.jpg
Images
This zip file contains the input image (test.pam) as well as the two output images (test.png and test.jpg).
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
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
Some tools may convert it automatically.
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
- https://github.com/cjw-network/cjwpublish1411/blob/master/vendor/imagine/imagine/lib/Imagine/resources/Adobe/CMYK/USWebCoatedSWOP.icc
- https://github.com/saucecontrol/Compact-ICC-Profiles/blob/master/profiles/sRGB-v4.icc
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.)
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.
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.
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
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.
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.
There is no automated method currently other than a script. Use the profiles that I suggested.
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.pngwithout 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.
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.)
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.
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).
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 sRGBand-profile sRGB-v4.iccchanges 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.
... 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.
Regardless of how the colors are encoded in the output file, they should look the same, right?
No, different output profiles may look different.
Ok, it seems that adding a warning is the only thing that IM needs to do.
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.