diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index 5ad40b011..40a027cc5 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -30,7 +30,7 @@ def helper_save_as_pdf(tmp_path, mode, **kwargs): with open(outfile, "rb") as fp: contents = fp.read() size = tuple( - int(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split() + float(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split() ) assert im.size == size @@ -86,6 +86,27 @@ def test_unsupported_mode(tmp_path): im.save(outfile) +def test_resolution(tmp_path): + im = hopper() + + outfile = str(tmp_path / "temp.pdf") + im.save(outfile, resolution=150) + + with open(outfile, "rb") as fp: + contents = fp.read() + + size = tuple( + float(d) + for d in contents.split(b"stream\nq ")[1].split(b" 0 0 cm")[0].split(b" 0 0 ") + ) + assert size == (61.44, 61.44) + + size = tuple( + float(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split() + ) + assert size == (61.44, 61.44) + + @mark_if_feature_version( pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing" ) diff --git a/src/PIL/PdfImagePlugin.py b/src/PIL/PdfImagePlugin.py index 2ba4671d5..49ba077e6 100644 --- a/src/PIL/PdfImagePlugin.py +++ b/src/PIL/PdfImagePlugin.py @@ -202,8 +202,8 @@ def _save(im, fp, filename, save_all=False): MediaBox=[ 0, 0, - int(width * 72.0 / resolution), - int(height * 72.0 / resolution), + width * 72.0 / resolution, + height * 72.0 / resolution, ], Contents=contents_refs[pageNumber], ) @@ -211,9 +211,9 @@ def _save(im, fp, filename, save_all=False): # # page contents - page_contents = b"q %d 0 0 %d 0 0 cm /image Do Q\n" % ( - int(width * 72.0 / resolution), - int(height * 72.0 / resolution), + page_contents = b"q %f 0 0 %f 0 0 cm /image Do Q\n" % ( + width * 72.0 / resolution, + height * 72.0 / resolution, ) existing_pdf.write_obj(contents_refs[pageNumber], stream=page_contents) diff --git a/src/PIL/PdfParser.py b/src/PIL/PdfParser.py index 86d78a95c..b5279e0d7 100644 --- a/src/PIL/PdfParser.py +++ b/src/PIL/PdfParser.py @@ -330,6 +330,8 @@ def pdf_repr(x): return bytes(x) elif isinstance(x, int): return str(x).encode("us-ascii") + elif isinstance(x, float): + return str(x).encode("us-ascii") elif isinstance(x, time.struct_time): return b"(D:" + time.strftime("%Y%m%d%H%M%SZ", x).encode("us-ascii") + b")" elif isinstance(x, dict):