
- Python Pillow 教程
- Python Pillow - 首頁
- Python Pillow - 概述
- Python Pillow - 環境設定
- 基本影像操作
- Python Pillow - 處理影像
- Python Pillow - 調整影像大小
- Python Pillow - 翻轉和旋轉影像
- Python Pillow - 裁剪影像
- Python Pillow - 為影像新增邊框
- Python Pillow - 識別影像檔案
- Python Pillow - 合併影像
- Python Pillow - 剪下和貼上影像
- Python Pillow - 滾動影像
- Python Pillow - 在影像上寫入文字
- Python Pillow - ImageDraw 模組
- Python Pillow - 連線兩張影像
- Python Pillow - 建立縮圖
- Python Pillow - 建立水印
- Python Pillow - 影像序列
- Python Pillow 顏色轉換
- Python Pillow - 影像上的顏色
- Python Pillow - 使用顏色建立影像
- Python Pillow - 將顏色字串轉換為 RGB 顏色值
- Python Pillow - 將顏色字串轉換為灰度值
- Python Pillow - 透過更改畫素值來更改顏色
- 影像處理
- Python Pillow - 降噪
- Python Pillow - 更改影像模式
- Python Pillow - 影像合成
- Python Pillow - 使用 Alpha 通道
- Python Pillow - 應用透視變換
- 影像濾鏡
- Python Pillow - 為影像新增濾鏡
- Python Pillow - 卷積濾鏡
- Python Pillow - 模糊影像
- Python Pillow - 邊緣檢測
- Python Pillow - 浮雕影像
- Python Pillow - 增強邊緣
- Python Pillow - 逆濾鏡
- 影像增強和校正
- Python Pillow - 增強對比度
- Python Pillow - 增強銳度
- Python Pillow - 增強顏色
- Python Pillow - 校正色彩平衡
- Python Pillow - 去噪
- 影像分析
- Python Pillow - 提取影像元資料
- Python Pillow - 識別顏色
- 高階主題
- Python Pillow - 建立動畫 GIF
- Python Pillow - 批次處理影像
- Python Pillow - 轉換影像檔案格式
- Python Pillow - 為影像新增填充
- Python Pillow - 顏色反轉
- Python Pillow - 使用 NumPy 進行機器學習
- Python Pillow 與 Tkinter BitmapImage 和 PhotoImage 物件
- Image 模組
- Python Pillow - 影像混合
- Python Pillow 有用資源
- Python Pillow - 快速指南
- Python Pillow - 函式參考
- Python Pillow - 有用資源
- Python Pillow - 討論
Python Pillow - 建立動畫 GIF
GIF(圖形交換格式)是一種點陣圖影像格式,由 CompuServe(一家線上服務提供商)的一個團隊在美籍計算機科學家史蒂夫·威爾海特(Steve Wilhite)的領導下開發。GIF 最初並非設計為動畫媒介。然而,它能夠在一個檔案中儲存多張影像,這使得它成為表示動畫序列幀的合乎邏輯的選擇。為了支援動畫的呈現,GIF89a 規範引入了圖形控制擴充套件(GCE)。此擴充套件允許指定每個幀的時間延遲,從而有效地允許從一系列影像建立影片剪輯。
在動畫 GIF 中,每個幀都由其自己的 GCE 引入,指定在繪製幀後應發生的時間延遲。此外,在檔案開頭定義的全域性資訊用作所有幀的預設設定,簡化了動畫設定和行為的管理。
Python 的 Pillow 庫可以讀取 GIF87a 和 GIF89a 格式的 GIF 檔案。預設情況下,它以 GIF87a 格式寫入 GIF 檔案,除非使用了 GIF89a 功能或輸入檔案已經是 GIF89a 格式。儲存的檔案使用 LZW 編碼。
使用 Python Pillow 建立動畫 GIF
可以使用 Pillow 的 Image.save() 函式建立動畫 GIF。以下是呼叫 save() 函式以儲存 GIF 檔案時的語法和可用選項:
語法
Image.save(out, save_all=True, append_images=[im1, im2, ...])
選項
save_all - 如果設定為 true,則儲存影像的所有幀。否則,僅儲存多幀影像的第一幀。
append_images - 此選項允許將影像列表附加為附加幀。列表中的影像可以是單幀或多幀影像。此功能支援 GIF、PDF、PNG、TIFF 和 WebP 格式,以及 ICO 和 ICNS 格式。當提供相關大小的影像時,它們將用於代替縮小主影像。
include_color_table - 確定是否包含本地顏色表。
interlace - 指定影像是否交錯。預設情況下,啟用交錯,除非影像的寬度或高度小於 16 畫素。
disposal - 指示在顯示圖形後如何處理圖形。可以將其設定為 0(未指定處理)、1(不處理)、2(恢復為背景顏色)或 3(恢復為先前內容)等值。您可以為常量處理傳遞單個整數,或傳遞列表/元組以分別為每個幀設定處理。
palette - 此選項允許您為儲存的影像使用指定的調色盤。調色盤應作為包含 RGBRGB... 格式的調色盤條目的位元組或位元組陣列物件提供。它不應超過 768 個位元組。或者,您可以將調色盤作為 PIL.ImagePalette.ImagePalette 物件傳遞。
optimize - 如果設定為 true,則嘗試透過消除未使用的顏色來壓縮調色盤。當調色盤可以壓縮到下一個較小的 2 的冪元素時,此最佳化很有用。
可以提供諸如透明度、持續時間、迴圈和註釋之類的其他選項來控制動畫 GIF 的特定方面。
示例
這是一個示例,演示瞭如何透過生成具有不同顏色的單個幀並儲存它們來建立 GIF 動畫。
import numpy as np from PIL import Image # Function to create a new image with a specified width, height, and color def create_image(width, height, color): return Image.new("RGBA", (width, height), color) # Set the width and height of the images width, height = 300, 300 # Define the colors for the images colors = [(64, 64, 3), (255, 0, 0), (255, 255, 0), (255, 255, 255), (164, 0, 3)] # Create a list of images using a list comprehension images = [create_image(width, height, color) for color in colors] # Save the images as a GIF with specified parameters images[0].save("Output.gif", save_all=True, append_images=images[1:], duration=1000/2, loop=0)
輸出
The animated GIFs file is saved successfully...
您可以在工作目錄中看到儲存的動畫 GIF:

示例
以下示例獲取現有影像檔案的列表,透過按順序儲存這些影像來建立動畫 GIF。
from PIL import Image # List of file paths for existing images image_paths = ['Images/book_1.jpg', 'Images/book_2.jpg', 'Images/book_3.jpg', 'Images/book_4.jpg'] # Create a list of image objects from the provided file paths image_list = [Image.open(path) for path in image_paths] # Save the first image as an animated GIF output_path = \Book_Animation.gif' image_list[0].save( output_path, save_all=True, append_images=image_list[1:], # Append the remaining images duration=1000, # Frame duration in milliseconds loop=0 ) print('The animated GIF file has been created and saved successfully...')
輸出
The animated GIF file has been created and saved successfully...
下圖顯示了儲存在工作目錄中的動畫 GIF:

示例
此示例透過將現有 GIF 檔案的最後一幀複製幾次,然後將其另存為新的 GIF 檔案來修改現有 GIF 檔案。在此示例中,我們將使用 ImageSequence 模組來迭代輸入 GIF 檔案中的每個幀。
from PIL import Image, ImageSequence # Open the existing GIF file input_image = Image.open("Book_Animation.gif") # Create an empty list to store the frames of the GIF frames = [] # Iterate over the frames of the GIF and append them to the frames list for frame in ImageSequence.Iterator(input_image): frames.append(frame) # Duplicate the last frame three times to extend the animation for i in range(3): frames.append(frames[-1]) # Save the frames as a new GIF file ("newGif.gif"): output_path = "newGif.gif" frames[0].save( output_path, save_all=True, append_images=frames[1:], optimize=False, duration=40, # Set the frame duration to 40 milliseconds loop=0 )
輸出
下圖顯示了儲存在工作目錄中的動畫 GIF:
