Python Pillow - 影像序列



影像序列簡介

Pillow(Python 影像庫)中的影像序列指的是按特定順序顯示的一組單個影像,以建立動畫。影像序列的常用檔案格式包括 GIF、APNG(動畫行動式網路圖形)、FLI/FLC、TIFF 檔案(包含多個幀)等等。

Python Pillow 庫提供了ImageSequence模組來處理影像序列和動畫圖片。ImageSequence 模組的主要功能是能夠迭代影像序列的幀。我們可以使用ImageSequence.Iterator類來實現這一點,它充當影像序列的包裝器,簡化了訪問單個幀的過程。

讀取影像序列

讀取影像序列是處理動畫或多幀影像時的基本操作。您可以使用 Python Pillow (PIL) 庫中的Image.open()方法開啟和讀取影像序列。

示例

這是一個演示如何使用 Python Pillow Image.open() 方法讀取影像序列的示例。

from PIL import Image, ImageSequence

# Open the image sequence file
image = Image.open("Images/Book_animation.gif")

# Display the opened image
image.show()

輸出

執行上述程式碼時,我們將得到以下輸出:

imageseuence allframes

開啟影像序列檔案時,PIL 會自動載入第一幀。要訪問序列中的其他幀,我們需要使用ImageSequence.Iterator類迭代它們,該類提供了一種方便的方法來迴圈遍歷影像序列中的所有幀。

訪問影像序列中的幀

Python Pillow 中的ImageSequence.Iterator()類允許我們迭代影像序列的幀。它接受一個影像物件作為引數,並實現一個迭代器物件,使用者可以使用該物件迭代影像序列。我們可以使用運算子訪問序列中的單個幀,也可以使用 for 迴圈迭代所有幀。

使用ImageSequence.Iterator()類的語法如下:

image_object = PIL.ImageSequence.Iterator(image)

其中image_object是用於迴圈遍歷影像序列幀的迭代器,image是我們的輸入影像物件(PIL.Image 的例項)。

要在 Pillow 中使用 ImageSequence.Iterator,請按照以下步驟操作:

  • 匯入必要的模組,例如 Image 和 ImageSequence。

  • 使用Image.open()方法載入序列中的第一張影像。

  • 使用PIL.ImageSequence.Iterator()類迭代幀。

  • 在迴圈中,根據需要處理或操作每一幀,這可能包括任何以下操作,例如調整大小、新增文字、應用濾鏡或 Pillow 中可用的其他影像處理技術。

  • 使用save()方法將處理後的幀另存為單獨的影像。

示例

以下示例演示瞭如何使用ImageSequence.Iterator()類迭代影像序列的幀。

from PIL import Image, ImageSequence

# Open an image sequence file
image = Image.open("Images/Book_animation.gif")

index = 0

# Iterate through the frames
for frame in ImageSequence.Iterator(image):
    # Process or manipulate the current frame here
    frame.convert('RGB').save("output_image/frame % d.jpg" % index)
    index += 1

print("All frames of the ImageSequence are saved separately.")

輸出

All frames of the ImageSequence are saved separately.

執行上述程式碼後,影像序列的所有幀將分別儲存在您的工作目錄中。

imageseuence ex2

我們還可以訪問影像序列中各個幀的各種屬性。例如,我們可以檢索每個幀的持續時間,這決定了它在動畫中顯示的時間長度。

建立影像序列

雖然 Image Sequence 模組的主要用途是從現有序列中提取幀,但我們也可以建立自己的影像序列。在此處瞭解更多關於建立動畫 GIF 的資訊

示例

這是一個演示如何從影像列表建立 GIF 影像序列的基本示例。

from PIL import Image

# Create a list of image objects
images = [Image.open(f"Images/split{i}.jpg") for i in range(1,4)]

# Save the image sequence as a GIF
images[0].save(
    "output_image/creating_animation.gif",
    save_all=True,
    append_images=images[1:],
    duration=200,
    loop=0
)

print("Image sequence created and saved as creating_animation.gif")

輸出

Image sequence created and saved as creating_animation.gif
image_sequences_ex3 gif

修改和儲存影像序列

影像序列可以像普通影像一樣進行處理和儲存。要修改影像序列,您可以使用 Pillow 的 ImageSequence 模組中的all_frames()方法。此方法將指定函式應用於影像序列中的所有幀,並返回一個包含所有分離幀的列表。使用此方法,我們可以迭代影像序列中的每一幀並應用必要的變換。

以下是 ImageSequence.all_frames() 方法的語法:

PIL.ImageSequence.all_frames(im,func)

其中,

  • im - 一個影像序列。

  • func - 要應用於所有影像幀的函式。

對影像序列進行更改後,我們可以根據需要將其儲存為新的影像序列或不同的格式。

示例

此示例演示瞭如何為影像序列的每一幀新增文字並儲存修改後的序列。

from PIL import Image, ImageSequence, ImageDraw

# Open the image sequence
im = Image.open('Images/book_animation.gif')

# Define a function to add text to each frame
def add_text(im_frame):
    draw = ImageDraw.Draw(im_frame)
    draw.text((150, 100), "Tutorialspoint", fill='green')
    return im_frame

# Apply the add_text function to all frames in the image sequence
ims = ImageSequence.all_frames(im, add_text)

# Save the modified frames as a new GIF
ims[0].convert('RGB').save('output_image/modified_image_sequences.gif', save_all=True, append_images=ims[1:])
print('Text added to each frame of the given ImageSequence.')

輸出

Text added to each frame of the given ImageSequence.
modified_image_sequences

結論

Pillow 的 Image Sequence 模組是處理影像序列和建立動畫的強大工具。我們可以輕鬆地迭代幀、提取屬性並應用各種影像操作來建立動態視覺效果。無論我們是處理 GIF、建立自定義動畫還是處理影像序列,Pillow 都提供了必要的函式和類來簡化此過程。

廣告