If DPI is invalid, ignore it instead of raising an error

This commit is contained in:
Andrew Murray 2021-08-01 18:38:56 +10:00
parent 5f4653d0b4
commit ae54838146
3 changed files with 7 additions and 6 deletions

BIN
Tests/images/zero_dpi.jp2 Normal file

Binary file not shown.

View File

@ -155,6 +155,9 @@ def test_load_dpi():
with Image.open("Tests/images/test-card-lossless.jp2") as im:
assert im.info["dpi"] == (71.9836, 71.9836)
with Image.open("Tests/images/zero_dpi.jp2") as im:
assert "dpi" not in im.info
def test_layers_type(tmp_path):
outfile = str(tmp_path / "temp_layers.jp2")

View File

@ -131,11 +131,8 @@ def _res_to_dpi(num, denom, exp):
"""Convert JPEG2000's (numerator, denominator, exponent-base-10) resolution,
calculated as (num / denom) * 10^exp and stored in dots per meter,
to floating-point dots per inch."""
if num == 0 or denom == 0:
raise SyntaxError(
f"Invalid JP2 resolution information: ({num} / {denom}) * 10^{exp}"
)
return (254 * num * (10 ** exp)) / (10000 * denom)
if denom != 0:
return num / denom * (10 ** exp) * 0.0254
def _parse_jp2_header(fp):
@ -217,7 +214,8 @@ def _parse_jp2_header(fp):
vrcn, vrcd, hrcn, hrcd, vrce, hrce = res.read_fields(">HHHHBB")
hres = _res_to_dpi(hrcn, hrcd, hrce)
vres = _res_to_dpi(vrcn, vrcd, vrce)
dpi = (hres, vres)
if hres is not None and vres is not None:
dpi = (hres, vres)
if size is None or mode is None:
raise SyntaxError("Malformed JP2 header")