[英]Can't save list of Pillow images as PDF?
我在一个名为pages
的列表中有一个 PIL ImageFiles 列表,如下所示:
[<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'
我不知道我做错了什么。 我正在使用 Python 3.8.5 和 Pillow 7.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 和所有随机图像。
我能够使用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:])
您不能像这样修改 pdf。 创建 Pdf 是为了表示具有严格映射内容的物理纸张。 事实上,Adobe 发明了非常数字化的纸张标准,并将其设为严格的只读格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.