PNG: Cannot have Palette images with ICC profile #107

Closed
opened 2021-06-13 13:31:10 +00:00 by Frans · 6 comments

while this seems to be a valid PNG file, img2pdf gives 'error: Cannot have Palette images with ICC profile'

while this seems to be a valid PNG file, img2pdf gives 'error: Cannot have Palette images with ICC profile'
Owner

Hrm... this is indeed an oversight of mine. Thanks for finding this bug!

The following patch should fix the problem:

--- a/src/img2pdf.py
+++ b/src/img2pdf.py
@@ -823,7 +823,9 @@ class pdfdoc(object):
             elif color == Colorspace.CMYK or color == Colorspace["CMYK;I"]:
                 iccpdict[PdfName.N] = 4
             elif color == Colorspace.P:
-                raise Exception("Cannot have Palette images with ICC profile")
+                # Only the png format can have an icc profile as well as a
+                # color palette. A png palette is always RGB.
+                iccpdict[PdfName.N] = 3
             colorspace = [PdfName.ICCBased, iccpdict]
 
         # either embed the whole jpeg or deflate the bitmap representation

Curiously, you are the first person with a palette PNG image that comes with an ICC profile. Do you know which software produces these? The metadata of your image suggests this comes from ghostscript?

Hrm... this is indeed an oversight of mine. Thanks for finding this bug! The following patch should fix the problem: ```diff --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -823,7 +823,9 @@ class pdfdoc(object): elif color == Colorspace.CMYK or color == Colorspace["CMYK;I"]: iccpdict[PdfName.N] = 4 elif color == Colorspace.P: - raise Exception("Cannot have Palette images with ICC profile") + # Only the png format can have an icc profile as well as a + # color palette. A png palette is always RGB. + iccpdict[PdfName.N] = 3 colorspace = [PdfName.ICCBased, iccpdict] # either embed the whole jpeg or deflate the bitmap representation ``` Curiously, you are the first person with a palette PNG image that comes with an ICC profile. Do you know which software produces these? The metadata of your image suggests this comes from ghostscript?
Author

Thanks for your quick answer and solution! I am not into all these formats, so did not know PNG is always RGB.
I do not know which software the image was produced, it is a page of a book which the publisher published as individual PNG pages. I selected a blank page, which made clear it was not due to the (visible) contents of the page.

Thanks for your quick answer and solution! I am not into all these formats, so did not know PNG is always RGB. I do not know which software the image was produced, it is a page of a book which the publisher published as individual PNG pages. I selected a blank page, which made clear it was not due to the (visible) contents of the page.
Owner

Okay, awesome. The fix should be part of the next img2pdf release. If you do not want to wait that long, feel free to apply above patch to your local copy. Thanks again!

Okay, awesome. The fix should be part of the next img2pdf release. If you do not want to wait that long, feel free to apply above patch to your local copy. Thanks again!
josch closed this issue 2021-06-14 16:34:54 +00:00
Frans reopened this issue 2021-06-15 18:09:27 +00:00
Author

unfortunally there is still a problem: the resulting pdf is reported as not valid by Acrobat Reader: 'due to an error on the page the page is possibly not correctly displayed'.

unfortunally there is still a problem: the resulting pdf is reported as not valid by Acrobat Reader: 'due to an error on the page the page is possibly not correctly displayed'.
Owner

Ahaa! I'm a donkey. Thanks for re-opening this. The original error message "Cannot have Palette images with ICC profile" makes sense. In the PDF format you specify the colorspace to either be ICCBased or Indexed aka palette-based. So we cannot store a palette PNG with an ICC profile in PDF directly. Instead, we have to convert the palette PNG to a normal RGB-png to fix this bug.

Ahaa! I'm a donkey. Thanks for re-opening this. The original error message "Cannot have Palette images with ICC profile" makes sense. In the PDF format you specify the colorspace to either be ICCBased **or** Indexed aka palette-based. So we cannot store a palette PNG with an ICC profile in PDF directly. Instead, we have to convert the palette PNG to a normal RGB-png to fix this bug.
Author

I am so happy that you KNOW these intricate file-format details and share your knowledge by providing such a good library! Many years ago I made a tiff-conversion thing for a project and that was difficult enough. From the little I know form the pdf format is that it is very difficult due to a version built-up of 'features' and backward compatibillity.
Many thanks for looking into this!

I am so happy that you KNOW these intricate file-format details and share your knowledge by providing such a good library! Many years ago I made a tiff-conversion thing for a project and that was difficult enough. From the little I know form the pdf format is that it is very difficult due to a version built-up of 'features' and backward compatibillity. Many thanks for looking into this!
josch closed this issue 2021-10-03 19:06:19 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: josch/img2pdf#107
No description provided.