ZeroDivisionError: float division by zero #192

Open
opened 2 months ago by newadventure · 5 comments

Version img2pdf 0.5.1 from homebrew

Platform: macOS

/opt/homebrew/bin/img2pdf -v '/Users/me/Downloads/62'/*.jpg --output '/Users/me/Downloads/62.pdf' 2>&1
DEBUG:img2pdf:PIL format = JPEG
DEBUG:img2pdf:imgformat = JPEG
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace = RGB
DEBUG:img2pdf:width x height = 1988px x 2821px
DEBUG:img2pdf:read_images() embeds a JPEG
DEBUG:img2pdf:PIL format = JPEG
DEBUG:img2pdf:imgformat = JPEG
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace = RGB
DEBUG:img2pdf:width x height = 1988px x 2816px
DEBUG:img2pdf:read_images() embeds a JPEG
DEBUG:img2pdf:PIL format = JPEG
DEBUG:img2pdf:imgformat = JPEG
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace = RGB
DEBUG:img2pdf:width x height = 1983px x 2816px
DEBUG:img2pdf:read_images() embeds a JPEG
DEBUG:img2pdf:PIL format = JPEG
DEBUG:img2pdf:imgformat = JPEG
DEBUG:img2pdf:input dpi = 300 x 0
DEBUG:PIL.TiffImagePlugin:tag: XResolution (282) - type: rational (5) Tag Location: 22 - Data Location: 26 - value: b',\x01\x00\x00\x01\x00\x00\x00'
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace = RGB
DEBUG:img2pdf:width x height = 3964px x 2782px
DEBUG:img2pdf:read_images() embeds a JPEG
ERROR:img2pdf:error: float division by zero
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/img2pdf/0.5.1_1/libexec/lib/python3.12/site-packages/img2pdf.py", line 4499, in main
    convert(
  File "/opt/homebrew/Cellar/img2pdf/0.5.1_1/libexec/lib/python3.12/site-packages/img2pdf.py", line 2740, in convert
    pagewidth, pageheight, imgwidthpdf, imgheightpdf = kwargs["layout_fun"](
                                                       ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/img2pdf/0.5.1_1/libexec/lib/python3.12/site-packages/img2pdf.py", line 2576, in default_layout_fun
    imgheightpdf = pageheight = px_to_pt(imgheightpx, ndpi[1])
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/img2pdf/0.5.1_1/libexec/lib/python3.12/site-packages/img2pdf.py", line 2323, in px_to_pt
    return 72.0 * length / dpi
           ~~~~~~~~~~~~~~^~~~~
ZeroDivisionError: float division by zero

I viewed all images and none are corrupted or of 0 size/length

Version `img2pdf 0.5.1` from homebrew Platform: macOS ``` /opt/homebrew/bin/img2pdf -v '/Users/me/Downloads/62'/*.jpg --output '/Users/me/Downloads/62.pdf' 2>&1 DEBUG:img2pdf:PIL format = JPEG DEBUG:img2pdf:imgformat = JPEG DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace = RGB DEBUG:img2pdf:width x height = 1988px x 2821px DEBUG:img2pdf:read_images() embeds a JPEG DEBUG:img2pdf:PIL format = JPEG DEBUG:img2pdf:imgformat = JPEG DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace = RGB DEBUG:img2pdf:width x height = 1988px x 2816px DEBUG:img2pdf:read_images() embeds a JPEG DEBUG:img2pdf:PIL format = JPEG DEBUG:img2pdf:imgformat = JPEG DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace = RGB DEBUG:img2pdf:width x height = 1983px x 2816px DEBUG:img2pdf:read_images() embeds a JPEG DEBUG:img2pdf:PIL format = JPEG DEBUG:img2pdf:imgformat = JPEG DEBUG:img2pdf:input dpi = 300 x 0 DEBUG:PIL.TiffImagePlugin:tag: XResolution (282) - type: rational (5) Tag Location: 22 - Data Location: 26 - value: b',\x01\x00\x00\x01\x00\x00\x00' DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace = RGB DEBUG:img2pdf:width x height = 3964px x 2782px DEBUG:img2pdf:read_images() embeds a JPEG ERROR:img2pdf:error: float division by zero Traceback (most recent call last): File "/opt/homebrew/Cellar/img2pdf/0.5.1_1/libexec/lib/python3.12/site-packages/img2pdf.py", line 4499, in main convert( File "/opt/homebrew/Cellar/img2pdf/0.5.1_1/libexec/lib/python3.12/site-packages/img2pdf.py", line 2740, in convert pagewidth, pageheight, imgwidthpdf, imgheightpdf = kwargs["layout_fun"]( ^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/img2pdf/0.5.1_1/libexec/lib/python3.12/site-packages/img2pdf.py", line 2576, in default_layout_fun imgheightpdf = pageheight = px_to_pt(imgheightpx, ndpi[1]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/img2pdf/0.5.1_1/libexec/lib/python3.12/site-packages/img2pdf.py", line 2323, in px_to_pt return 72.0 * length / dpi ~~~~~~~~~~~~~~^~~~~ ZeroDivisionError: float division by zero ``` I viewed all images and none are corrupted or of 0 size/length
josch commented 2 months ago
Owner

Can you provide me with an image that exhibits this behaviour? Thanks!

Can you provide me with an image that exhibits this behaviour? Thanks!
josch commented 2 months ago
Owner

Also, the division by zero error comes from one of your images which has:

DEBUG:img2pdf:input dpi = 300 x 0

I wonder what img2pdf should do when it encounters such an image. According to your debug output, it should be the fourth image matching /Users/me/Downloads/62'/*.jpg.

Also, the division by zero error comes from one of your images which has: DEBUG:img2pdf:input dpi = 300 x 0 I wonder what img2pdf should do when it encounters such an image. According to your debug output, it should be the fourth image matching `/Users/me/Downloads/62'/*.jpg`.
Poster

Yes, that's correct. macOS Preview opens it fine and its Inspector says the DPI is 300 pixels/inch

imagemagick thinks it's OK too:

identify -format '%x,%y\n' '/Users/awinn/Downloads/_Errors/error.jpg'
300,72

The image is NSFW. Can I email it to you?

Yes, that's correct. macOS Preview opens it fine and its Inspector says the DPI is 300 pixels/inch imagemagick thinks it's OK too: ``` identify -format '%x,%y\n' '/Users/awinn/Downloads/_Errors/error.jpg' 300,72 ``` The image is NSFW. Can I email it to you?
josch commented 2 months ago
Owner

Yes, my private email is josch@mister-muffin.de.

Thank you for helping with getting to the bottom of this!

Yes, my private email is josch@mister-muffin.de. Thank you for helping with getting to the bottom of this!
josch commented 2 months ago
Owner

Okay, I have an analysis for this issue. Here are the first 32 bytes of the problematic image:

00000000: ffd8 ffe0 0010 4a46 4946 0001 0101 012c  ......JFIF.....,
00000010: 0000 0000 ffe1 002a 4578 6966 0000 4949  .......*Exif..II

This is the JFIF APP0 segment and the density unit can be found at offset 13 and is 1 which corresponds to dots per inch. The two bytes after that are 0x12c which is 300 in decimal and the X resolution. The two bytes after that are the Y resolution and set to zero. This is why your image is problematic.

Okay, I have an analysis for this issue. Here are the first 32 bytes of the problematic image: ``` 00000000: ffd8 ffe0 0010 4a46 4946 0001 0101 012c ......JFIF....., 00000010: 0000 0000 ffe1 002a 4578 6966 0000 4949 .......*Exif..II ``` This is the JFIF APP0 segment and the density unit can be found at offset 13 and is 1 which corresponds to dots per inch. The two bytes after that are 0x12c which is 300 in decimal and the X resolution. The two bytes after that are the Y resolution and set to zero. This is why your image is problematic.
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#192
Loading…
There is no content yet.