簡體   English   中英

無法將枕頭圖像列表保存為 PDF?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM