16bit depth png, got error: Refusing to work with multiple >8bit channels. #170

Closed
opened 10 months ago by leoleozhu · 4 comments

Hi
When I convert some pngs to PDF, I get the following error.

img2pdf 4.png -o 4.pdf

DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13
DEBUG:PIL.PngImagePlugin:STREAM b'iCCP' 41 345
DEBUG:PIL.PngImagePlugin:iCCP profile name b'Display P3'
DEBUG:PIL.PngImagePlugin:Compression method 0
DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 398 9
DEBUG:PIL.PngImagePlugin:STREAM b'iTXt' 419 1523
DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 1954 99089
DEBUG:img2pdf:PIL format = PNG
DEBUG:img2pdf:imgformat = PNG
WARNING:img2pdf:Image with transparency and a bit depth of 16.
WARNING:img2pdf:This is unsupported due to PIL limitations.
ERROR:img2pdf:error: Refusing to work with multiple >8bit channels.
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/img2pdf.py", line 4062, in main
    convert(
  File "/usr/local/lib/python3.11/site-packages/img2pdf.py", line 2360, in convert
    ) in read_images(
         ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/img2pdf.py", line 1673, in read_images
    color, ndpi, imgwidthpx, imgheightpx, rotation, iccp = get_imgmetadata(
                                                           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/img2pdf.py", line 1318, in get_imgmetadata
    raise AlphaChannelError(
img2pdf.AlphaChannelError: Refusing to work with multiple >8bit channels.

In the code, there states that PIL will lossily convert 16-bit images to 8-bit, so img2pdf refuses to work with those images.

If I'm happy with the lossily convertion, could you please suggest some steps to continue the conversion?

Env

MacOS 13.4
Python 3.11
img2pdf 0.4.4

Hi When I convert some pngs to PDF, I get the following error. ``` img2pdf 4.png -o 4.pdf DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13 DEBUG:PIL.PngImagePlugin:STREAM b'iCCP' 41 345 DEBUG:PIL.PngImagePlugin:iCCP profile name b'Display P3' DEBUG:PIL.PngImagePlugin:Compression method 0 DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 398 9 DEBUG:PIL.PngImagePlugin:STREAM b'iTXt' 419 1523 DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 1954 99089 DEBUG:img2pdf:PIL format = PNG DEBUG:img2pdf:imgformat = PNG WARNING:img2pdf:Image with transparency and a bit depth of 16. WARNING:img2pdf:This is unsupported due to PIL limitations. ERROR:img2pdf:error: Refusing to work with multiple >8bit channels. Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/img2pdf.py", line 4062, in main convert( File "/usr/local/lib/python3.11/site-packages/img2pdf.py", line 2360, in convert ) in read_images( ^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/img2pdf.py", line 1673, in read_images color, ndpi, imgwidthpx, imgheightpx, rotation, iccp = get_imgmetadata( ^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/img2pdf.py", line 1318, in get_imgmetadata raise AlphaChannelError( img2pdf.AlphaChannelError: Refusing to work with multiple >8bit channels. ``` In the code, there states that PIL will lossily convert 16-bit images to 8-bit, so img2pdf refuses to work with those images. If I'm happy with the lossily convertion, could you please suggest some steps to continue the conversion? **Env** MacOS 13.4 Python 3.11 img2pdf 0.4.4
Owner

You are right, the warning should include what to do if one is fine with lossy conversion. How about the following patch:

diff --git a/src/img2pdf.py b/src/img2pdf.py
index 54701c9..79256b4 100755
--- a/src/img2pdf.py
+++ b/src/img2pdf.py
@@ -1322,6 +1322,7 @@ def get_imgmetadata(
             if depth > 8:
                 logger.warning("Image with transparency and a bit depth of %d." % depth)
                 logger.warning("This is unsupported due to PIL limitations.")
+                logger.warning("If you accept a lossy conversion, you can manually convert your images to 8 bit using `convert -depth 8` from imagemagick")
                 raise AlphaChannelError(
                     "Refusing to work with multiple >8bit channels."
                 )
You are right, the warning should include what to do if one is fine with lossy conversion. How about the following patch: ```diff diff --git a/src/img2pdf.py b/src/img2pdf.py index 54701c9..79256b4 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -1322,6 +1322,7 @@ def get_imgmetadata( if depth > 8: logger.warning("Image with transparency and a bit depth of %d." % depth) logger.warning("This is unsupported due to PIL limitations.") + logger.warning("If you accept a lossy conversion, you can manually convert your images to 8 bit using `convert -depth 8` from imagemagick") raise AlphaChannelError( "Refusing to work with multiple >8bit channels." ) ```

(Or theoretically one could add an option allowing img2pdf to continue lossily if explicitly requested by the user, but I know you want to keep img2pdf lossless-only, and probably you don't want to bloat it with too many new options, either.)

(Or theoretically one could add an option allowing img2pdf to continue lossily if explicitly requested by the user, but I know you want to keep img2pdf lossless-only, and probably you don't want to bloat it with too many new options, either.)
Owner

Yes, what @mara0004 said is correct. If somebody wants to do lossly conversion, then imagemagick already exists and can take care of this. No need for img2pdf if lossy is fine for you. There are plenty of other tools that will do the job just fine.

Yes, what @mara0004 said is correct. If somebody wants to do lossly conversion, then imagemagick already exists and can take care of this. No need for img2pdf if lossy is fine for you. There are plenty of other tools that will do the job just fine.
Poster

Thank you and the warning patch looks good. I will check the depth of the image first, if it's not 8 bit, use imagemagick to convert it first. Then use img2pdf to convert it to PDF.

Thank you and the warning patch looks good. I will check the depth of the image first, if it's not 8 bit, use imagemagick to convert it first. Then use img2pdf to convert it to PDF.
leoleozhu closed this issue 10 months ago
Sign in to join this conversation.
No Milestone
No project
No Assignees
3 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#170
Loading…
There is no content yet.