From 3bbd0a58109c840a8895f364ffc7127cee04a30b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 18 May 2018 22:15:45 +1000 Subject: [PATCH] Fixed saving a multiframe image as a single frame PDF --- Tests/test_file_pdf.py | 10 ++++++++++ src/PIL/PdfImagePlugin.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index f17da8d74..3d359f445 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -104,6 +104,16 @@ class TestFilePdf(PillowTestCase): self.assertTrue(os.path.isfile(outfile)) self.assertGreater(os.path.getsize(outfile), 0) + def test_multiframe_normal_save(self): + # Test saving a multiframe image without save_all + im = Image.open("Tests/images/dispose_bgnd.gif") + + outfile = self.tempfile('temp.pdf') + im.save(outfile) + + self.assertTrue(os.path.isfile(outfile)) + self.assertGreater(os.path.getsize(outfile), 0) + def test_pdf_open(self): # fail on a buffer full of null bytes self.assertRaises(PdfParser.PdfFormatError, PdfParser.PdfParser, buf=bytearray(65536)) diff --git a/src/PIL/PdfImagePlugin.py b/src/PIL/PdfImagePlugin.py index e8e0c4f3b..8538bcd49 100644 --- a/src/PIL/PdfImagePlugin.py +++ b/src/PIL/PdfImagePlugin.py @@ -113,7 +113,8 @@ def _save(im, fp, filename, save_all=False): pageNumber = 0 for imSequence in ims: - for im in ImageSequence.Iterator(imSequence): + im_pages = ImageSequence.Iterator(imSequence) if save_all else [imSequence] + for im in im_pages: # FIXME: Should replace ASCIIHexDecode with RunLengthDecode (packbits) # or LZWDecode (tiff/lzw compression). Note that PDF 1.2 also supports # Flatedecode (zip compression).