Images with ICC profile silently converted to sRGB profile
If a color image has a color profile, img2pdf will silently discard it instead of preserving it.
The example image is a PNG set to the ProPhoto RGB profile, with the profile embedded. Pillow is capable of extracting it.
The PDF produced by img2pdf will have the profile dropped, which means it is assumed to be sRGB. As a result, it will look like "washed-out purple" rather than "fuschia-pink".
Also included is a PDF repaired with pikepdf to include the appropriate color profile. This is for the case of a paletted (/Indexed) image.
I think this is a duplicate of https://gitlab.mister-muffin.de/josch/img2pdf/issues/65
I just rebased the patch onto recent img2pdf (especially with the new test suite) and am checking:
if 'icc_profile' in imgdata.info: iccp = imgdata.info.get('icc_profile')
I'm still struggeling with testing this though. With the help of @iwata74 I'm now able to use imagemagick to compare the pdf as rendered by ghostscript like so:
$ img2pdf png_with_icc.png -o out.pdf $ gs -dQUIET -dNOPAUSE -dBATCH -sDEVICE=png16m -r96 -sOutputFile="out-gs.png" out.pdf $ convert -profile reverse.icc png_with_icc.png png_reversed.png $ compare -metric AE out-gs.png png_reversed.png
Under Debian unstable I created
/usr/share/color/icc/sRGB.iccembedded in it and used
reverse.iccin the commands above.
Strangely enough, this procedure only works when rendering the pdf as created by img2pdf with ghostscript. If I use mupdf, poppler or pdfimages, I get slightly different results which are not exact anymore.
I'm also puzzled by the fact that I cannot distinguish your two files pink-palette-icc-img2pdf.pdf and pink-palette-icc-correct.pdf from each other -- which pdf viewer are you using?
My first try in implementing this is in commit f0b57985 but as you can see, compare_poppler, compare_mupdf and compare_pdfimages_png are commented out because I could not get them to compare exactly. Do you know what the problem could be?
Mentioned in issue #65
If you can't distinguish the files your PDF viewer or system is not color managed. The difference should be pretty stark.
Here is how it looks to me:
I double-checked them in Acrobat and macOS Preview. Firefox cannot tell the difference (it does not have color managed PDF, but does have color managed images).If it's still an issue I believe there are test ICC profiles that exchange RGB->GBR or something, so it's immediately obvious if the color is wrong. I could see about regenerating with those.
When comparing, one or more of those programs may not apply color profiles correctly or might round them differently when they are applied, because it can be a lossy conversion.
Thanks a lot for going through the trouble of posting a screenshot. Indeed I see a difference but to my eyes, it's really minor, so it would not surprise me, if I am just either really bad at distinguishing these colors or my laptop monitor is really bad or both. XD But of course what counts is not that I am able to distinguish these but that the testsuite is able to distinguish these.
The idea of using a test profile sounds very intriguing and very useful. Then even people with bad eyes and/or monitors like me would be able to easily spot a problem. :)
I found this, for example and that looks useful, but unfortunately, the used ICC profile is proprietary. Do you know of some test profiles under a free license? Or maybe it's really simple to auto-generate a test profile?
I was now playing around with channel-swapping ICC profiles and noticed, that my image viewer (feh) ignores ICC profiles embedded in PNG images -- no wonder I never saw a difference! Firefox seems to be okay with ICC profiles in png images, but ignores them in PDF files. My normal PDF viewer (mupdf) also ignores embedded profiles while evince takes them into account. What a mess! So no wonder nothing worked for me if my normal image viewer as well as my normal pdf viewer both are ignorant about ICC profiles.
Thanks for the color-swapping tip -- that made me finally find out all this. :)Edited
@jbarlow83 sorry to bother you again, but I somehow have to make sure that I'm not crazy. Take these two images:
The first image has no embedded ICC profile. The second one contains a profile that switches colors. In firefox, those two PNG images, indeed look different, so it seems the profile embedded in the second gets respected. But if I open the second image (the one with the profile) with
displayfrom imagemagick, then it looks exactly like the first. Even worse:
compare -metric AE icc.png no-icc.png null: 0
So even imagemagick claims that those two images are absolutely identical even though they should not be. How am I supposed to write proper tests for PNG images and PDF files if the tooling is absolutely incapable of working with ICC profiles? You write that you are working with Acrobat and MacOS preview but I'm afraid that's a no-go for the testsuite of img2pdf. Do you have any suggestions for the tools I should use to properly work with images and PDFs containing ICC profiles?
Or maybe I'm making a mistake with imagemagick? I find it extremely weird, that the most popular image manipulation software cannot handle this?
Well, if imagemagick were competent at everything, img2pdf wouldn't need to exist, would it? :)
I think imagemagick ignores the color profile in some cases.
But I think your tests are "backwards" too: it would be easier to have images with different numerical values, that with the correct color profile will present as the same wavelengths of light. You want to switch the colors, and apply a profile that switches them back to normal.
Try these ones, which are permissively licensed and could be included in your test suite. They conveniently have text that becomes visible only if something is wrong: http://displaycal.net/icc-color-management-test/
@jbarlow83 thank you for your input!
The original issue got fixed with the 0.4.0 upload, right?
Actually I would prefer it, if there existed other software that was able to losslessly turn a raster graphic into a pdf, because then I'd have to do less work. That's why in the README I compare img2pdf to other software. I'm not writing software just to stay busy -- I have enough better things to do. The only reason I maintain img2pdf is, because I do not see any other software that is able to do the same thing, sadly. In an ideal future, imagemagick would be able to do the same thing img2pdf is able to do and then I could finally put img2pdf into the attic.
Thanks for the additional resource containing ICC profiles (and images) for testing purposes, but even though they are distributed under a free license, to me they are still binary blobs and even a binary blob under a free license, does not give me the freedom to change and improve it. So instead I investigated how to create my own color channel switching ICC profiles from scratch in commit 11907242 -- the colors get switch just fine but I'm not able to make it work with imagemagick without using the ghostscript sRGB profile.
Yes, the tests are backwards. As you say, I should first switch the color channels with imagemagick and then correct them using the ICC profile. Unfortunately, I had no luck with that approach. This might be either due to imagemagick or due to my inability to use it right.