PNG: Cannot have Palette images with ICC profile #107

Closed
opened 3 years ago by Frans · 6 comments
Frans commented 3 years ago

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'
josch commented 3 years ago
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?
Frans commented 3 years ago
Poster

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.
josch commented 3 years ago
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 3 years ago
Frans reopened this issue 3 years ago
Frans commented 3 years ago
Poster

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'.
josch commented 3 years ago
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.
Frans commented 3 years ago
Poster

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 3 years ago
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
Loading…
There is no content yet.