Skip to content

Do not require ResolutionUnit to determine JPEG DPI from XResolution - #10353

Merged
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:dpi
Oct 29, 2026
Merged

radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:dpi

Conversation

@radarhere

Copy link
Copy Markdown
Member

When determining DPI from EXIF data, JpegImagePlugin requires the ResolutionUnit tag.

def _read_dpi_from_exif(self) -> None:
# If DPI isn't in JPEG header, fetch from EXIF
if "dpi" in self.info or "exif" not in self.info:
return
try:
exif = self.getexif()
resolution_unit = exif[0x0128]

However, it is only used if it is 3 / cm
if resolution_unit == 3: # cm

So let's make it optional.

@Muguett-DBY Muguett-DBY left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The missing-unit case now reads the stored resolution, and the existing fallbacks held in my checks: explicit centimetres still convert by 2.54, absent XResolution and a zero-denominator rational still give 72 DPI, and JFIF density still takes precedence over EXIF.

I compared the exact base/head _read_dpi_from_exif methods on 16 serialized EXIF cases and three controls per ref, using the existing Pillow 13.0 JPEG codec/parser. All 38 results matched those expectations. This was a focused check, not a full branch build or pytest run. Codex-assisted.

@radarhere
radarhere merged commit 2bef844 into python-pillow:main Oct 29, 2026
51 checks passed
@radarhere
radarhere deleted the dpi branch October 29, 2026 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants