ImageSequence 模組

ImageSequence 模組包含一個包裝類別,可讓您迭代影像序列的幀。

從動畫中提取幀

from PIL import Image, ImageSequence

with Image.open("animation.fli") as im:
    index = 1
    for frame in ImageSequence.Iterator(im):
        frame.save(f"frame{index}.png")
        index += 1

Iterator 類別

class PIL.ImageSequence.Iterator(im: Image)[原始碼]

這個類別實作一個迭代器物件,可以用來迴圈遍歷影像序列。

您可以使用 [] 運算子依索引存取元素。如果您嘗試存取不存在的幀,此運算子會引發 IndexError

參數:

im – 一個影像物件。

函式

PIL.ImageSequence.all_frames(im: Image | list[Image], func: Callable[[Image], Image] | None = None) list[Image][原始碼]

將給定的函式應用於影像或影像列表中的所有幀。這些幀會以單獨影像的列表形式傳回。

參數:
  • im – 一個影像或影像列表。

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

傳回:

一個影像列表。