Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save an image list in PDF using PIL (pillow)?

I want use PIL .save() method for export my PIL image list to pdf.

in the PIL document , saving part say: enter image description here => we can use append_images option for pdf format.

and in pillow's github page , this issue say : Added append_images to PDF saving #2526

I wrote this code:

import PIL
im1 = PIL.Image.open("1.jpg").convert("RGB")
im2 = PIL.Image.open("2.jpg").convert("RGB")
im3 = PIL.Image.open("3.jpg").convert("RGB")
images = [im1,im2,im3]
images[0].save("out.pdf", save_all=True, append_images=images[1:])

but it doesn't work!

These errors raised:

Traceback (most recent call last):
  File "sample.py", line 13, in <module>
gif.save("out.pdf", save_all=True, append_images=images)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 1928, in save
save_handler(self, fp, filename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/PdfImagePlugin.py", line 55, in _save_all
_save(im, fp, filename, save_all=True)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/PdfImagePlugin.py", line 182, in _save
Image.SAVE["JPEG"](im, op, filename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 609, in _save
info = im.encoderinfo
AttributeError: 'Image' object has no attribute 'encoderinfo'
like image 449
mkal Avatar asked Nov 15 '17 13:11

mkal


People also ask

How do I save a PIL library image?

save() Saves this image under the given filename. If no format is specified, the format to use is determined from the filename extension, if possible. Keyword options can be used to provide additional instructions to the writer.

What is PIL image format?

Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.

Does Pillow support PDF?

A list of images to append as additional frames. Each of the images in the list can be single or multiframe images. This is currently supported for GIF, PDF, PNG, TIFF, and WebP. It is also supported for ICO and ICNS.


1 Answers

Try this format

from PIL import Image
im1 = PIL.Image.open("1.jpg").convert("RGB")
im2 = PIL.Image.open("2.jpg").convert("RGB")
im3 = PIL.Image.open("3.jpg").convert("RGB")
images = [im2,im3]
im1.save("out.pdf", save_all=True, append_images=images)
like image 60
murty kalyan Avatar answered Oct 01 '22 19:10

murty kalyan