【发布时间】:2018-04-28 17:52:47
【问题描述】:
我想使用 PIL .save() 方法将我的 PIL 图像列表导出为 pdf。
在 PIL document 中,保存部分说:
=> 我们可以将append_images 选项用于pdf格式。
在枕头的 github 页面中,this issue 说:Added append_images to PDF saving #2526
我写了这段代码:
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:])
但它不起作用!
出现这些错误:
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'
【问题讨论】:
-
微不足道,但你应该改写
append_images=images。您还可以将导入语句更改为from PIL import Image,然后使用Image.open(..)。 -
@VasilisG。好的。但错误没有解决! :|
-
看看here。除此之外,我想不出别的了。
-
PDF 格式不是图像格式(尽管它可以包含其中的一个或多个),因此 PIL/Pillow 不知道如何在其中保存图像格式。您需要使用其他一些模块来创建 PDF 文件(并且可能允许您向其中添加图像)。
reportlab可能是一个不错的选择。 -
我认为这是一个错误。在 Python 3.6.0 上使用 Pillow 4.3.0 我可以轻松保存单个 PDF 图像。我可以使用
append_imagesarg 调用.save方法来编写正确的多帧GIF。如果源图像是 GIF,我什至可以保存多帧 PDF,但是当我检查生成的 PDF 文件时,它是不正确的。我在测试中使用了 3 张图像,输出 PDF 由 3 页组成,每列包含 3 张图像,顶部和底部图像似乎是我列表中的第一张图像,中间图像是错误解码的版本我的第一张图片。 :(
标签: python pdf python-imaging-library