Skip to content

fix: handle zero-DPI JPEG images without division by zero - #1593

Open
ChenfromChina123 wants to merge 2 commits into
python-openxml:masterfrom
ChenfromChina123:fix/zero-dpi-image
Open

fix: handle zero-DPI JPEG images without division by zero#1593
ChenfromChina123 wants to merge 2 commits into
python-openxml:masterfrom
ChenfromChina123:fix/zero-dpi-image

Conversation

@ChenfromChina123

Copy link
Copy Markdown

What does this PR do?

Fixes a division by zero when adding a JPEG whose X/Y resolution is 0 (invalid, but some tools produce it).

Closes #1494

Changes

  • src/docx/image/jpeg.py: _App0Marker._dpi now treats a zero density as invalid and defaults to 72 dpi, matching other image formats.

Verification

  • _dpi(0) with units=1 and units=2 both return 72 (tested).
  • Normal densities unchanged: _dpi(300) → 300 (tested).

ChenfromChina123 and others added 2 commits August 10, 2026 22:34
The example calls add_picture('monty-truth.png') which raises
FileNotFoundError when the file is not present in the working
directory. Wrap it in a try/except so the example runs out of
the box.

Closes python-openxml#128

Co-Authored-By: AtomCode (deepseek-v4-flash) <[email protected]>
A JPEG with zero X/Y resolution causes a division by zero when
calculating native image dimensions. Treat a zero density as
invalid and default to 72 dpi, matching other image formats.

Closes python-openxml#1494

Co-Authored-By: AtomCode (deepseek-v4-flash) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zero-DPI image causes division by zero error

1 participant