Removed DPI rounding from JPEG loading

This commit is contained in:
Andrew Murray 2021-05-07 21:23:29 +10:00
parent 0de3beaeaf
commit 18e204df05
3 changed files with 5 additions and 11 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

View File

@ -656,15 +656,6 @@ class TestFileJpeg:
reloaded.load() reloaded.load()
assert im.info["dpi"] == reloaded.info["dpi"] 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): def test_save_dpi_rounding(self, tmp_path):
outfile = str(tmp_path / "temp.jpg") outfile = str(tmp_path / "temp.jpg")
with Image.open("Tests/images/hopper.jpg") as im: with Image.open("Tests/images/hopper.jpg") as im:

View File

@ -33,6 +33,7 @@
# #
import array import array
import io import io
import math
import os import os
import struct import struct
import subprocess import subprocess
@ -162,15 +163,17 @@ def APP(self, marker):
dpi = float(x_resolution[0]) / x_resolution[1] dpi = float(x_resolution[0]) / x_resolution[1]
except TypeError: except TypeError:
dpi = x_resolution dpi = x_resolution
if math.isnan(dpi):
raise ValueError
if resolution_unit == 3: # cm if resolution_unit == 3: # cm
# 1 dpcm = 2.54 dpi # 1 dpcm = 2.54 dpi
dpi *= 2.54 dpi *= 2.54
self.info["dpi"] = int(dpi + 0.5), int(dpi + 0.5) self.info["dpi"] = dpi, dpi
except (KeyError, SyntaxError, ValueError, ZeroDivisionError): except (KeyError, SyntaxError, ValueError, ZeroDivisionError):
# SyntaxError for invalid/unreadable EXIF # SyntaxError for invalid/unreadable EXIF
# KeyError for dpi not included # KeyError for dpi not included
# ZeroDivisionError for invalid dpi rational value # 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 self.info["dpi"] = 72, 72