我在一个名为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
您不能像这样修改pdf。创建Pdf是为了表示具有严格映射内容的物理纸张。事实上,Adobe发明了非常数字的纸张标准,他们严格地将其设置为只读格式
我可以用
append_images
参数复制这个问题,不知道为什么它不能像文档中描述的那样工作。 解决方法是逐个添加页面。这对我很有用:找出
append_images
不起作用的原因会很有趣事实上,有了
save_all=True
它对我很有效相关问题 更多 >
编程相关推荐