Skip to content

Commit

Permalink
Removed DPI rounding from JPEG loading
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 7, 2021
1 parent 0de3bea commit 18e204d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
Binary file removed Tests/images/iptc_roundDown.jpg
Binary file not shown.
9 changes: 0 additions & 9 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,6 @@ def test_save_tiff_with_dpi(self, tmp_path):
reloaded.load()
assert im.info["dpi"] == reloaded.info["dpi"]

def test_load_dpi_rounding(self):
# Round up
with Image.open("Tests/images/iptc_roundUp.jpg") as im:
assert im.info["dpi"] == (44, 44)

# Round down
with Image.open("Tests/images/iptc_roundDown.jpg") as im:
assert im.info["dpi"] == (2, 2)

def test_save_dpi_rounding(self, tmp_path):
outfile = str(tmp_path / "temp.jpg")
with Image.open("Tests/images/hopper.jpg") as im:
Expand Down
7 changes: 5 additions & 2 deletions src/PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#
import array
import io
import math
import os
import struct
import subprocess
Expand Down Expand Up @@ -162,15 +163,17 @@ def APP(self, marker):
dpi = float(x_resolution[0]) / x_resolution[1]
except TypeError:
dpi = x_resolution
if math.isnan(dpi):
raise ValueError
if resolution_unit == 3: # cm
# 1 dpcm = 2.54 dpi
dpi *= 2.54
self.info["dpi"] = int(dpi + 0.5), int(dpi + 0.5)
self.info["dpi"] = dpi, dpi
except (KeyError, SyntaxError, ValueError, ZeroDivisionError):
# SyntaxError for invalid/unreadable EXIF
# KeyError for dpi not included
# ZeroDivisionError for invalid dpi rational value
# ValueError for x_resolution[0] being an invalid float
# ValueError for dpi being an invalid float
self.info["dpi"] = 72, 72


Expand Down

0 comments on commit 18e204d

Please sign in to comment.