Skip to content

Commit 06c03d6

Browse files
committed
fix(image): fallback to 72 dpi when header dpi is 0 (closes #1497, #1494)
1 parent e454546 commit 06c03d6

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

HISTORY.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
Release History
44
---------------
55

6+
Unreleased
7+
++++++++++
8+
9+
* **Fixed** – Issues #1497 / #1494: adding an image whose header reports
10+
`dpi = 0` now falls back to `72 dpi` instead of raising
11+
`ZeroDivisionError` when using `Document.add_picture()`.
12+
13+
614
1.2.0 (2025-06-16)
715
++++++++++++++++++
816

src/docx/image/image.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,27 @@ def px_height(self) -> int:
8787

8888
@property
8989
def horz_dpi(self) -> int:
90-
"""Integer dots per inch for the width of this image.
91-
92-
Defaults to 72 when not present in the file, as is often the case.
9390
"""
94-
return self._image_header.horz_dpi
91+
Horizontal DPI reported for this image. Returns the header value
92+
unless it is `None` **or** `0`, in which case it defaults to
93+
72 dpi, matching Word's internal assumption.
94+
"""
95+
dpi = self._image_header.horz_dpi
96+
if dpi in (None, 0):
97+
return 72
98+
return dpi
9599

96100
@property
97101
def vert_dpi(self) -> int:
98-
"""Integer dots per inch for the height of this image.
99-
100-
Defaults to 72 when not present in the file, as is often the case.
101102
"""
102-
return self._image_header.vert_dpi
103+
Vertical DPI reported for this image. Returns the header value
104+
unless it is `None` **or** `0`, in which case it defaults to
105+
72 dpi, matching Word's internal assumption.
106+
"""
107+
dpi = self._image_header.vert_dpi
108+
if dpi in (None, 0):
109+
return 72
110+
return dpi
103111

104112
@property
105113
def width(self) -> Inches:
@@ -219,16 +227,24 @@ def px_height(self):
219227

220228
@property
221229
def horz_dpi(self):
222-
"""Integer dots per inch for the width of this image.
223-
224-
Defaults to 72 when not present in the file, as is often the case.
225230
"""
226-
return self._horz_dpi
231+
Horizontal DPI reported for this image. Returns the header value
232+
unless it is `None` **or** `0`, in which case it defaults to
233+
72 dpi, matching Word's internal assumption.
234+
"""
235+
dpi = self._horz_dpi
236+
if dpi in (None, 0):
237+
return 72
238+
return dpi
227239

228240
@property
229241
def vert_dpi(self):
230-
"""Integer dots per inch for the height of this image.
231-
232-
Defaults to 72 when not present in the file, as is often the case.
233242
"""
234-
return self._vert_dpi
243+
Vertical DPI reported for this image. Returns the header value
244+
unless it is `None` **or** `0`, in which case it defaults to
245+
72 dpi, matching Word's internal assumption.
246+
"""
247+
dpi = self._vert_dpi
248+
if dpi in (None, 0):
249+
return 72
250+
return dpi

tests/test_files/zero_dpi.jpg

8.83 KB
Loading

tests/test_zero_dpi.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Regression test for issues #1497 and #1494 – ZeroDivisionError when adding a
2+
JPEG whose header reports 0 × 0 DPI.
3+
"""
4+
5+
from pathlib import Path
6+
7+
from docx import Document
8+
9+
FIX = Path(__file__).with_name("test_files") / "zero_dpi.jpg"
10+
11+
12+
class DescribeZeroDensityJPEG:
13+
"""Suite covering `Document.add_picture()` with 0-DPI images."""
14+
15+
def it_handles_zero_dpi(self):
16+
doc = Document()
17+
doc.add_picture(str(FIX))

0 commit comments

Comments
 (0)