Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,18 @@ def test_resolution(tmp_path):
assert size == (61.44, 61.44)


def test_dpi(tmp_path):
im = hopper()

outfile = str(tmp_path / "temp.pdf")
im.save(outfile, dpi=(75, 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 == (122.88, 61.44)

size = tuple(
float(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split()
)
assert size == (122.88, 61.44)


def test_resolution_and_dpi(tmp_path):
@pytest.mark.parametrize(
"params",
(
{"dpi": (75, 150)},
{"dpi": (75, 150), "resolution": 200},
),
)
def test_dpi(params, tmp_path):
im = hopper()

outfile = str(tmp_path / "temp.pdf")
im.save(outfile, resolution=200, dpi=(75, 150))
im.save(outfile, **params)

with open(outfile, "rb") as fp:
contents = fp.read()
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/PdfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ def _save(im, fp, filename, save_all=False):
else:
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")

x_resolution = y_resolution = im.encoderinfo.get("resolution", 72.0)

dpi = im.encoderinfo.get("dpi")
if dpi:
x_resolution = dpi[0]
y_resolution = dpi[1]
else:
x_resolution = y_resolution = im.encoderinfo.get("resolution", 72.0)

info = {
"title": None
Expand Down