Python Pillow 简明教程

Python Pillow - Image Sequences

Introduction to Image Sequences

Pillow(Python Imaging Library)中的图像序列是指按照特定顺序显示的一组单独图像,以创建动画。图像序列的流行文件格式包括 GIF、APNG(Animated Portable Network Graphics)、FLI/FLC、TIFF 文件(具有多个帧)等等。

Python Pillow 库提供了 ImageSequence 模块,用于处理图像序列和动画图片。这个 ImageSequence 模块的主要功能是,它能够遍历图像序列中的帧。我们可以通过使用 ImageSequence.Iterator 类来实现,该类充当图像序列的包装器,并简化了访问各个帧的过程。

Reading Image Sequences

在处理动画或多帧图像时,读取图像序列是一项基本操作。通过 Python Pillow(PIL)库中的 Image.open() 方法,您可以打开并读取图像序列。

Example

此处有一个示例,演示如何使用 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()

在执行上面的代码时,我们将得到以下输出 -

image sequences ex1

当打开一个图像序列文件时,PIL 会自动加载第一帧。要访问序列中的其他帧,我们需要使用 ImageSequence.Iterator 类遍历它们,该类提供了一种便利的方式来循环遍历图像序列中的所有帧。

Accessing Frames in an Image Sequence

Python Pillow 中的 ImageSequence.Iterator() 类允许我们遍历图像序列的帧。它接受一个图像对象作为参数,并实现了一个迭代器对象,用户可以使用它来遍历图像序列。我们可以使用操作符来访问序列中的各个帧,或者我们可以使用 for 循环来遍历所有帧。

使用 ImageSequence.Iterator() 类的语法如下 -

image_object = PIL.ImageSequence.Iterator(image)

其中 image_object 是用于循环播放图像序列帧的迭代器,而 image 是我们的输入图像对象(PIL.Image 的一个实例。)

要在 Pillow 中使用 ImageSequence.Iterator,请遵循以下步骤:

  1. 导入必要的模块,如 Image 和 ImageSequence。

  2. 使用 Image.open() 方法加载序列中的第一张图像。

  3. 使用 PIL.ImageSequence.Iterator() 类遍历帧。

  4. 在循环中,根据需要处理或操作每一帧,其中可以包括任何以下操作,如调整大小、添加文字、应用滤镜或其他在 Pillow 中可用的图像处理技术。

  5. 使用 save() 方法将处理后的帧另存为独立的图像。

Example

以下是使用 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.

执行以上代码后,图像序列中的所有帧都会分别保存到你的工作目录中。

image sequences ex2

我们还可以访问图像序列中单个帧的各种属性。例如,我们可以检索每一帧的持续时间,这决定了它在动画中显示多长时间。

Creating Image Sequences

虽然 Image Sequence 模块的主要用途是从现有序列中提取帧,但我们也可以创建自己的图像序列。了解更多关于 Creating Animated GIFs here

Example

这里有一个基本示例,演示了如何从一组图像创建一个 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

Modifying and Saving Image Sequences

图像序列可以像常规图像一样被操作和保存。对于修改图像序列,你可以使用 Pillow 的 ImageSequence 模块中的 all_frames() 方法。此方法对图像序列中的所有帧应用一个指定的功能,并返回包含所有独立帧的列表。通过使用此方法,我们可以遍历图像序列中的每一帧并应用必要的转换。

以下是 ImageSequence.all_frames() 方法的语法 −

PIL.ImageSequence.all_frames(im,func)

其中,

  1. im − 一个图像序列。

  2. func − 应用于图像序列中所有帧的一个函数。

对图像序列进行更改后,我们可以根据需要将其另存为新的图像序列或其他格式。

Example

此示例演示了如何向图像序列的每一帧添加文字,并保存修改后的序列。

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

Conclusion

Pillow 的 Image Sequence 模块是用于处理图像序列和创建动画的有力工具。我们可以轻松地遍历帧、提取属性并应用各种图像操作来创建动态视觉效果。无论我们是处理 GIF,还是创建自定义动画或处理图像序列,Pillow 都提供了必需的功能和类来简化流程。