mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-05-28 17:53:11 +03:00
If DPI is invalid, ignore it instead of raising an error
This commit is contained in:
parent
5f4653d0b4
commit
ae54838146
BIN
Tests/images/zero_dpi.jp2
Normal file
BIN
Tests/images/zero_dpi.jp2
Normal file
Binary file not shown.
|
@ -155,6 +155,9 @@ def test_load_dpi():
|
||||||
with Image.open("Tests/images/test-card-lossless.jp2") as im:
|
with Image.open("Tests/images/test-card-lossless.jp2") as im:
|
||||||
assert im.info["dpi"] == (71.9836, 71.9836)
|
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):
|
def test_layers_type(tmp_path):
|
||||||
outfile = str(tmp_path / "temp_layers.jp2")
|
outfile = str(tmp_path / "temp_layers.jp2")
|
||||||
|
|
|
@ -131,11 +131,8 @@ def _res_to_dpi(num, denom, exp):
|
||||||
"""Convert JPEG2000's (numerator, denominator, exponent-base-10) resolution,
|
"""Convert JPEG2000's (numerator, denominator, exponent-base-10) resolution,
|
||||||
calculated as (num / denom) * 10^exp and stored in dots per meter,
|
calculated as (num / denom) * 10^exp and stored in dots per meter,
|
||||||
to floating-point dots per inch."""
|
to floating-point dots per inch."""
|
||||||
if num == 0 or denom == 0:
|
if denom != 0:
|
||||||
raise SyntaxError(
|
return num / denom * (10 ** exp) * 0.0254
|
||||||
f"Invalid JP2 resolution information: ({num} / {denom}) * 10^{exp}"
|
|
||||||
)
|
|
||||||
return (254 * num * (10 ** exp)) / (10000 * denom)
|
|
||||||
|
|
||||||
|
|
||||||
def _parse_jp2_header(fp):
|
def _parse_jp2_header(fp):
|
||||||
|
@ -217,6 +214,7 @@ def _parse_jp2_header(fp):
|
||||||
vrcn, vrcd, hrcn, hrcd, vrce, hrce = res.read_fields(">HHHHBB")
|
vrcn, vrcd, hrcn, hrcd, vrce, hrce = res.read_fields(">HHHHBB")
|
||||||
hres = _res_to_dpi(hrcn, hrcd, hrce)
|
hres = _res_to_dpi(hrcn, hrcd, hrce)
|
||||||
vres = _res_to_dpi(vrcn, vrcd, vrce)
|
vres = _res_to_dpi(vrcn, vrcd, vrce)
|
||||||
|
if hres is not None and vres is not None:
|
||||||
dpi = (hres, vres)
|
dpi = (hres, vres)
|
||||||
|
|
||||||
if size is None or mode is None:
|
if size is None or mode is None:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user