Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';是否将枕头图像列表保存为PDF?_Python_Python 3.x_Python Imaging Library - Fatal编程技术网

Python Can';是否将枕头图像列表保存为PDF?

Python Can';是否将枕头图像列表保存为PDF?,python,python-3.x,python-imaging-library,Python,Python 3.x,Python Imaging Library,我在一个名为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

我在一个名为
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>] 
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:])
我不知道我做错了什么。我正在使用Python3.8.5和Pillow7.1.2。我还尝试了最新的PIL版本8.0.1

感谢您的帮助

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

编辑2:

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

[<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>] 
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发明了非常数字化的纸张标准,并且他们将其严格设置为只读格式。

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

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

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
参数复制问题,但不知道为什么它不能像文档中描述的那样工作。 解决方法是逐个添加页面。这对我很有用:

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')

尝试不使用
save_all
,例如
pages[0]。保存(“test2.pdf”,append_images=pages[1:])
,这样不会引发错误,但只会将pdf与第一页一起保存。这与只调用页面[0]完全相同。保存(“test.pdf”)不,不是。根据文档
附加图像
-附加为附加页面的图像列表。列表中的每个图像可以是单帧图像或多帧图像。“全部保存”仅适用于多帧图像(例如TIFF)。如果问题的答案是您的问题,请检查此链接。枕头无法将RGBA图像保存为pdf,请确保图像是RGB,请尝试不使用
save\u all
,例如
pages[0]。保存(“test2.pdf”,append\u images=pages[1:]
,这不会引发错误,但只保存带有第一页的pdf。这与只调用页面[0]完全相同。保存(“test.pdf”)不,不是。根据文档
附加图像
-附加为附加页面的图像列表。列表中的每个图像可以是单帧图像或多帧图像。save_all仅适用于多帧图像(例如TIFF)请检查此链接,如果它回答了您的问题,枕头无法将RGBA图像保存为pdf,请确保图像为RGB这既不正确,也不能回答问题。我熟悉ISO标准,谢谢,我已经同意Adobe设计了它。但我所说的是,PDF是只读的,这是不正确的,因为它们显然可以编写。此外,这并不能帮助OP写一份PDF,这正是他所要求的。事实上,我对从互联网边缘部分给出解决方案感觉不太好。特别是当官方文件决定不涵盖时。这既不正确,也不能回答问题。我熟悉ISO标准,谢谢,我已经同意Adobe设计了它。但我所说的是,PDF是只读的,这是不正确的,因为它们显然可以编写。此外,这并不能帮助OP写一份PDF,这正是他所要求的。事实上,我对从互联网边缘部分给出解决方案感觉不太好。特别是当官方文件决定不涵盖。