无法将枕头图像列表保存为PDF格式?

2024-04-19 05:54:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个名为pages的列表中有一个PIL图像文件列表,如下所示:

[<PIL.PngImagePlugin.PngImageFile image mode=RGB size=612x792 at 0x21DEE7E0AF0>, 
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=612x792 at 0x21DEE7E00A0>, 
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=612x792 at 0x21DEE7E0370>, 
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=612x792 at 0x21DEE7E02B0>, 
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=612x792 at 0x21DEE7E0160>] 

如果我做了pages[0].show(),我可以看到图像

如果我做了pages[0].save("test.png"),那就行了

如果我做了pages[0].save("test.pdf"),那也行

但是,我想将整个图像列表保存为一个PDF,其中每个图像都是一页

所以我做了: pages[0].save("test2.pdf", save_all=True, append_images=pages[1:])

但是,这会引发一个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\redact\AppData\Roaming\Python\Python38\site-packages\PIL\Image.py", line 2134, in save
    save_handler(self, fp, filename)
  File "C:\Users\redact\AppData\Roaming\Python\Python38\site-packages\PIL\PdfImagePlugin.py", line 41, in _save_all
    _save(im, fp, filename, save_all=True)
  File "C:\Users\redact\AppData\Roaming\Python\Python38\site-packages\PIL\PdfImagePlugin.py", line 117, in _save
    for im in im_pages:
  File "C:\Users\redact\AppData\Roaming\Python\Python38\site-packages\PIL\ImageSequence.py", line 49, in __next__
    self.im.seek(self.position)
  File "C:\Users\redact\AppData\Roaming\Python\Python38\site-packages\PIL\PngImagePlugin.py", line 731, in seek
    if not self._seek_check(frame):
  File "C:\Users\redact\AppData\Roaming\Python\Python38\site-packages\PIL\ImageFile.py", line 296, in _seek_check
    frame < self._min_frame
AttributeError: 'PngImageFile' object has no attribute '_min_frame'

我不知道我做错了什么。我正在使用Python3.8.5和Pillow7.1.2。我还尝试了最新的PIL版本8.0.1

感谢您的帮助

编辑1:不使用save_all=True调用它只保存第一个图像

编辑2:

我试着做一个这样的最低限度的例子:

import numpy as np
from PIL import Image

random_pixels = np.random.rand(100, 100, 3) * 255
images = [Image.fromarray(random_pixels.astype("uint8")) for i in range(10)]

images[0].save("test.pdf", save_all=True, append_images=images[1:])

然而,这个最简单的例子对我来说是可行的,它正确地保存了包含所有随机图像的PDF


Tags: inpilsavepackageslinesitepagesroaming
2条回答

您不能像这样修改pdf。创建Pdf是为了表示具有严格映射内容的物理纸张。事实上,Adobe发明了非常数字的纸张标准,他们严格地将其设置为只读格式

我可以用append_images参数复制这个问题,不知道为什么它不能像文档中描述的那样工作。 解决方法是逐个添加页面。这对我很有用:

spam = [PngImageFile('pil_red.png'), PngImageFile('pil_blue.png')]
print(spam)
# [<PIL.PngImagePlugin.PngImageFile image mode=RGB size=60x30 at 0x7F6B33A14128>, 
# <PIL.PngImagePlugin.PngImageFile image mode=RGB size=60x30 at 0x7F6B33A202E8>]


for img in spam:
    try:
        img.save('test.pdf', append=True)
    except FileNotFoundError: # if the file does not exists, save the first image
        img.save('test.pdf')

找出append_images不起作用的原因会很有趣

事实上,有了save_all=True它对我很有效

from PIL.PngImagePlugin import PngImageFile
from PIL import Image
    
img = Image.new('RGB', (60, 30), color = 'red')
img.save('pil_red.png')
img = Image.new('RGB', (60, 30), color = 'blue')
img.save('pil_blue.png')

spam = [PngImageFile('pil_red.png'), PngImageFile('pil_blue.png')]
print(spam)
# [<PIL.PngImagePlugin.PngImageFile image mode=RGB size=60x30 at 0x7F6B33A14128>, 
# <PIL.PngImagePlugin.PngImageFile image mode=RGB size=60x30 at 0x7F6B33A202E8>]
spam[0].save('test.pdf', save_all=True, append_images=spam[1:])

相关问题 更多 >