Skip to content

fix: fallback to 72 dpi when image header is 0 (#1494) #1505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
Release History
---------------

Unreleased
++++++++++

* **Fixed** – Issues #1497 / #1494: adding an image whose header reports
`dpi = 0` now falls back to `72 dpi` instead of raising
`ZeroDivisionError` when using `Document.add_picture()`.


1.2.0 (2025-06-16)
++++++++++++++++++

Expand Down
48 changes: 32 additions & 16 deletions src/docx/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,27 @@ def px_height(self) -> int:

@property
def horz_dpi(self) -> int:
"""Integer dots per inch for the width of this image.

Defaults to 72 when not present in the file, as is often the case.
"""
return self._image_header.horz_dpi
Horizontal DPI reported for this image. Returns the header value
unless it is `None` **or** `0`, in which case it defaults to
72 dpi, matching Word's internal assumption.
"""
dpi = self._image_header.horz_dpi
if dpi in (None, 0):
return 72
return dpi

@property
def vert_dpi(self) -> int:
"""Integer dots per inch for the height of this image.

Defaults to 72 when not present in the file, as is often the case.
"""
return self._image_header.vert_dpi
Vertical DPI reported for this image. Returns the header value
unless it is `None` **or** `0`, in which case it defaults to
72 dpi, matching Word's internal assumption.
"""
dpi = self._image_header.vert_dpi
if dpi in (None, 0):
return 72
return dpi

@property
def width(self) -> Inches:
Expand Down Expand Up @@ -219,16 +227,24 @@ def px_height(self):

@property
def horz_dpi(self):
"""Integer dots per inch for the width of this image.

Defaults to 72 when not present in the file, as is often the case.
"""
return self._horz_dpi
Horizontal DPI reported for this image. Returns the header value
unless it is `None` **or** `0`, in which case it defaults to
72 dpi, matching Word's internal assumption.
"""
dpi = self._horz_dpi
if dpi in (None, 0):
return 72
return dpi

@property
def vert_dpi(self):
"""Integer dots per inch for the height of this image.

Defaults to 72 when not present in the file, as is often the case.
"""
return self._vert_dpi
Vertical DPI reported for this image. Returns the header value
unless it is `None` **or** `0`, in which case it defaults to
72 dpi, matching Word's internal assumption.
"""
dpi = self._vert_dpi
if dpi in (None, 0):
return 72
return dpi
Binary file added tests/test_files/zero_dpi.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/test_zero_dpi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Regression test for issues #1497 and #1494 – ZeroDivisionError when adding a
JPEG whose header reports 0 × 0 DPI.
"""

from pathlib import Path

from docx import Document

FIX = Path(__file__).with_name("test_files") / "zero_dpi.jpg"


class DescribeZeroDensityJPEG:
"""Suite covering `Document.add_picture()` with 0-DPI images."""

def it_handles_zero_dpi(self):
doc = Document()
doc.add_picture(str(FIX))