Repository: python-pillow/Pillow

Issue:
Error while saving multiple JPEGs as a multi-page PDF
### What did you do?

I wanted to make a PDF out of multiple JPEG images, I loaded the JPEGs and try to save the first one as a PDF with the other images appended.

### What did you expect to happen?

I expected a multi-page PDF, each page being one of my input images.

### What actually happened?

I got the following error:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[...]/lib/python3.5/site-packages/PIL/Image.py", line 1933, in save
    save_handler(self, fp, filename)
  File "[...]/lib/python3.5/site-packages/PIL/PdfImagePlugin.py", line 60, in _save_all
    _save(im, fp, filename, save_all=True)
  File "[...]/lib/python3.5/site-packages/PIL/PdfImagePlugin.py", line 187, in _save
    Image.SAVE["JPEG"](im, op, filename)
  File "[...]/lib/python3.5/site-packages/PIL/JpegImagePlugin.py", line 609, in _save
    info = im.encoderinfo
AttributeError: 'JpegImageFile' object has no attribute 'encoderinfo'
```

### What versions of Pillow and Python are you using?

Pillow 4.3.0 on Python 3.5

### How to reproduce this and more

I had the problem with JPEGs of mine but even with two very simple JPEGs generated with imagemagick I get the errors. What's interesting is if I try to make a TIFF first I can then make a PDF.

```python
# The bug should be on any two random JPEGs, I generated the following to test:
# > convert -size 100x100 xc:#990000 a.jpeg
# > convert -size 100x100 xc:#009900 b.jpeg

from PIL import Image

Image.open('a.jpeg').save('test.pdf', save_all=True, append_images=[Image.open('b.jpeg')])
# AttributeError: 'JpegImageFile' object has no attribute 'encoderinfo'

a = Image.open('a.jpeg')
b = Image.open('b.jpeg')
a.save('test.pdf', save_all=True, append_images=[b]) # ERROR 
a.save('test.gif', save_all=True, append_images=[b]) # SUCCESS
a.save('test.pdf', save_all=True, append_images=[b]) # ERROR
a.save('test.tiff', save_all=True, append_images=[b]) # SUCCESS
a.save('test.pdf', save_all=True, append_images=[b]) # SUCCESS
```

Test JPEGs you can use:

![b](https://user-images.githubusercontent.com/5647930/31671519-6cae8b30-b35a-11e7-8335-e069187cb071.jpeg)
![a](https://user-images.githubusercontent.com/5647930/31671521-6cdb21a4-b35a-11e7-8ef0-998d4f6bd8f4.jpeg)
Error while saving multiple JPEGs as a multi-page PDF
### What did you do?

I wanted to make a PDF out of multiple JPEG images, I loaded the JPEGs and try to save the first one as a PDF with the other images appended.

### What did you expect to happen?

I expected a multi-page PDF, each page being one of my input images.

### What actually happened?

I got the following error:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[...]/lib/python3.5/site-packages/PIL/Image.py", line 1933, in save
    save_handler(self, fp, filename)
  File "[...]/lib/python3.5/site-packages/PIL/PdfImagePlugin.py", line 60, in _save_all
    _save(im, fp, filename, save_all=True)
  File "[...]/lib/python3.5/site-packages/PIL/PdfImagePlugin.py", line 187, in _save
    Image.SAVE["JPEG"](im, op, filename)
  File "[...]/lib/python3.5/site-packages/PIL/JpegImagePlugin.py", line 609, in _save
    info = im.encoderinfo
AttributeError: 'JpegImageFile' object has no attribute 'encoderinfo'
```

### What versions of Pillow and Python are you using?

Pillow 4.3.0 on Python 3.5

### How to reproduce this and more

I had the problem with JPEGs of mine but even with two very simple JPEGs generated with imagemagick I get the errors. What's interesting is if I try to make a TIFF first I can then make a PDF.

```python
# The bug should be on any two random JPEGs, I generated the following to test:
# > convert -size 100x100 xc:#990000 a.jpeg
# > convert -size 100x100 xc:#009900 b.jpeg

from PIL import Image

Image.open('a.jpeg').save('test.pdf', save_all=True, append_images=[Image.open('b.jpeg')])
# AttributeError: 'JpegImageFile' object has no attribute 'encoderinfo'

a = Image.open('a.jpeg')
b = Image.open('b.jpeg')
a.save('test.pdf', save_all=True, append_images=[b]) # ERROR 
a.save('test.gif', save_all=True, append_images=[b]) # SUCCESS
a.save('test.pdf', save_all=True, append_images=[b]) # ERROR
a.save('test.tiff', save_all=True, append_images=[b]) # SUCCESS
a.save('test.pdf', save_all=True, append_images=[b]) # SUCCESS
```

Test JPEGs you can use:

![b](https://user-images.githubusercontent.com/5647930/31671519-6cae8b30-b35a-11e7-8335-e069187cb071.jpeg)
![a](https://user-images.githubusercontent.com/5647930/31671521-6cdb21a4-b35a-11e7-8ef0-998d4f6bd8f4.jpeg)


Discussion:

Resolving patch:
diff --git a/PIL/PdfImagePlugin.py b/PIL/PdfImagePlugin.py
--- a/PIL/PdfImagePlugin.py
+++ b/PIL/PdfImagePlugin.py
@@ -139,7 +139,7 @@ def write(self, value):
         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()
+            append_im.encoderinfo = im.encoderinfo.copy()
             ims.append(append_im)
     numberOfPages = 0
     for im in ims:


Test patch:
diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py
--- a/Tests/test_file_pdf.py
+++ b/Tests/test_file_pdf.py
@@ -80,6 +80,13 @@ def test_save_all(self):
         self.assertTrue(os.path.isfile(outfile))
         self.assertGreater(os.path.getsize(outfile), 0)
 
+        # Append JPEG images
+        jpeg = Image.open("Tests/images/flower.jpg")
+        jpeg.save(outfile, save_all=True, append_images=[jpeg.copy()])
+
+        self.assertTrue(os.path.isfile(outfile))
+        self.assertGreater(os.path.getsize(outfile), 0)
+
 
 if __name__ == '__main__':
     unittest.main()