mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-25 06:34:48 +03:00
Added PDF 2d-resolution export
This commit is contained in:
parent
5a2ecd13dd
commit
eacdb3d843
|
@ -64,6 +64,10 @@ class TestFilePdf(PillowTestCase):
|
||||||
# Act / Assert
|
# Act / Assert
|
||||||
self.helper_save_as_pdf(mode)
|
self.helper_save_as_pdf(mode)
|
||||||
|
|
||||||
|
def test_resolution(self):
|
||||||
|
self.helper_save_as_pdf("RGB", resolution=300.5)
|
||||||
|
self.helper_save_as_pdf("RGB", resolution=(300, 150))
|
||||||
|
|
||||||
def test_unsupported_mode(self):
|
def test_unsupported_mode(self):
|
||||||
im = hopper("LA")
|
im = hopper("LA")
|
||||||
outfile = self.tempfile("temp_LA.pdf")
|
outfile = self.tempfile("temp_LA.pdf")
|
||||||
|
|
|
@ -1054,9 +1054,9 @@ The :py:meth:`~PIL.Image.Image.save` method can take the following keyword argum
|
||||||
.. versionadded:: 5.1.0
|
.. versionadded:: 5.1.0
|
||||||
|
|
||||||
**resolution**
|
**resolution**
|
||||||
Image resolution in DPI. This, together with the number of pixels in the
|
Image resolution in DPI either as a numeric value or ``(xdpi, ydpi)`` tuple.
|
||||||
image, will determine the physical dimensions of the page that will be
|
This, together with the number of pixels in the image, will determine the
|
||||||
saved in the PDF.
|
physical dimensions of the page that will be saved in the PDF.
|
||||||
|
|
||||||
**title**
|
**title**
|
||||||
The document’s title. If not appending to an existing PDF file, this will
|
The document’s title. If not appending to an existing PDF file, this will
|
||||||
|
|
|
@ -56,6 +56,13 @@ def _save(im, fp, filename, save_all=False):
|
||||||
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
|
existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
|
||||||
|
|
||||||
resolution = im.encoderinfo.get("resolution", 72.0)
|
resolution = im.encoderinfo.get("resolution", 72.0)
|
||||||
|
# Allows the user to specify either a 1D resolution or 2D resolution.
|
||||||
|
if not isinstance(resolution, (list, tuple)):
|
||||||
|
pdf_width_res = resolution
|
||||||
|
pdf_height_res = resolution
|
||||||
|
else:
|
||||||
|
pdf_width_res = resolution[0]
|
||||||
|
pdf_height_res = resolution[1]
|
||||||
|
|
||||||
info = {
|
info = {
|
||||||
"title": None if is_appending else os.path.splitext(
|
"title": None if is_appending else os.path.splitext(
|
||||||
|
@ -211,8 +218,8 @@ def _save(im, fp, filename, save_all=False):
|
||||||
MediaBox=[
|
MediaBox=[
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
int(width * 72.0 / resolution),
|
int(width * 72.0 / pdf_width_res),
|
||||||
int(height * 72.0 / resolution)
|
int(height * 72.0 / pdf_height_res)
|
||||||
],
|
],
|
||||||
Contents=contents_refs[pageNumber])
|
Contents=contents_refs[pageNumber])
|
||||||
|
|
||||||
|
@ -221,8 +228,8 @@ def _save(im, fp, filename, save_all=False):
|
||||||
|
|
||||||
page_contents = PdfParser.make_bytes(
|
page_contents = PdfParser.make_bytes(
|
||||||
"q %d 0 0 %d 0 0 cm /image Do Q\n" % (
|
"q %d 0 0 %d 0 0 cm /image Do Q\n" % (
|
||||||
int(width * 72.0 / resolution),
|
int(width * 72.0 / pdf_width_res),
|
||||||
int(height * 72.0 / resolution)))
|
int(height * 72.0 / pdf_height_res)))
|
||||||
|
|
||||||
existing_pdf.write_obj(contents_refs[pageNumber],
|
existing_pdf.write_obj(contents_refs[pageNumber],
|
||||||
stream=page_contents)
|
stream=page_contents)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user