mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-05-26 00:33:06 +03:00
Added append_images to PDF saving
This commit is contained in:
parent
770cdc6aa6
commit
80806d349f
|
@ -20,7 +20,7 @@
|
|||
# Image plugin for PDF images (output only).
|
||||
##
|
||||
|
||||
from . import Image, ImageFile
|
||||
from . import Image, ImageFile, ImageSequence
|
||||
from ._binary import i8
|
||||
import io
|
||||
|
||||
|
@ -133,13 +133,24 @@ def _save(im, fp, filename, save_all=False):
|
|||
|
||||
#
|
||||
# pages
|
||||
numberOfPages = 1
|
||||
ims = [im]
|
||||
if save_all:
|
||||
append_images = im.encoderinfo.get("append_images", [])
|
||||
for append_im in append_images:
|
||||
if append_im.mode != im.mode:
|
||||
append_im = append_im.convert(im.mode)
|
||||
append_im.encoderinfo = im.encoderinfo.copy()
|
||||
ims.append(append_im)
|
||||
numberOfPages = 0
|
||||
for im in ims:
|
||||
im_numberOfPages = 1
|
||||
if save_all:
|
||||
try:
|
||||
numberOfPages = im.n_frames
|
||||
im_numberOfPages = im.n_frames
|
||||
except AttributeError:
|
||||
# Image format does not have n_frames. It is a single frame image
|
||||
pass
|
||||
numberOfPages += im_numberOfPages
|
||||
pages = [str(pageNumber*3+4)+" 0 R"
|
||||
for pageNumber in range(0, numberOfPages)]
|
||||
|
||||
|
@ -151,9 +162,9 @@ def _save(im, fp, filename, save_all=False):
|
|||
Kids="["+"\n".join(pages)+"]")
|
||||
_endobj(fp)
|
||||
|
||||
for pageNumber in range(0, numberOfPages):
|
||||
im.seek(pageNumber)
|
||||
|
||||
pageNumber = 0
|
||||
for imSequence in ims:
|
||||
for im in ImageSequence.Iterator(imSequence):
|
||||
#
|
||||
# image
|
||||
|
||||
|
@ -236,6 +247,8 @@ def _save(im, fp, filename, save_all=False):
|
|||
|
||||
_endobj(fp)
|
||||
|
||||
pageNumber += 1
|
||||
|
||||
#
|
||||
# trailer
|
||||
startxref = fp.tell()
|
||||
|
|
|
@ -74,6 +74,12 @@ class TestFilePdf(PillowTestCase):
|
|||
self.assertTrue(os.path.isfile(outfile))
|
||||
self.assertGreater(os.path.getsize(outfile), 0)
|
||||
|
||||
# Append images
|
||||
im.save(outfile, save_all=True, append_images=[hopper()])
|
||||
|
||||
self.assertTrue(os.path.isfile(outfile))
|
||||
self.assertGreater(os.path.getsize(outfile), 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user