Skip to content

Commit 0836e38

Browse files
authored
Merge pull request #5459 from radarhere/pdf_float
Do not round dimensions when saving PDF
2 parents 1c52f67 + bc935f9 commit 0836e38

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

Tests/test_file_pdf.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def helper_save_as_pdf(tmp_path, mode, **kwargs):
3030
with open(outfile, "rb") as fp:
3131
contents = fp.read()
3232
size = tuple(
33-
int(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split()
33+
float(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split()
3434
)
3535
assert im.size == size
3636

@@ -86,6 +86,27 @@ def test_unsupported_mode(tmp_path):
8686
im.save(outfile)
8787

8888

89+
def test_resolution(tmp_path):
90+
im = hopper()
91+
92+
outfile = str(tmp_path / "temp.pdf")
93+
im.save(outfile, resolution=150)
94+
95+
with open(outfile, "rb") as fp:
96+
contents = fp.read()
97+
98+
size = tuple(
99+
float(d)
100+
for d in contents.split(b"stream\nq ")[1].split(b" 0 0 cm")[0].split(b" 0 0 ")
101+
)
102+
assert size == (61.44, 61.44)
103+
104+
size = tuple(
105+
float(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split()
106+
)
107+
assert size == (61.44, 61.44)
108+
109+
89110
@mark_if_feature_version(
90111
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
91112
)

src/PIL/PdfImagePlugin.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,18 @@ def _save(im, fp, filename, save_all=False):
202202
MediaBox=[
203203
0,
204204
0,
205-
int(width * 72.0 / resolution),
206-
int(height * 72.0 / resolution),
205+
width * 72.0 / resolution,
206+
height * 72.0 / resolution,
207207
],
208208
Contents=contents_refs[pageNumber],
209209
)
210210

211211
#
212212
# page contents
213213

214-
page_contents = b"q %d 0 0 %d 0 0 cm /image Do Q\n" % (
215-
int(width * 72.0 / resolution),
216-
int(height * 72.0 / resolution),
214+
page_contents = b"q %f 0 0 %f 0 0 cm /image Do Q\n" % (
215+
width * 72.0 / resolution,
216+
height * 72.0 / resolution,
217217
)
218218

219219
existing_pdf.write_obj(contents_refs[pageNumber], stream=page_contents)

src/PIL/PdfParser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ def pdf_repr(x):
330330
return bytes(x)
331331
elif isinstance(x, int):
332332
return str(x).encode("us-ascii")
333+
elif isinstance(x, float):
334+
return str(x).encode("us-ascii")
333335
elif isinstance(x, time.struct_time):
334336
return b"(D:" + time.strftime("%Y%m%d%H%M%SZ", x).encode("us-ascii") + b")"
335337
elif isinstance(x, dict):

0 commit comments

Comments
 (0)