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 都提供了必要的函数和类来简化此过程。

广告
© . All rights reserved.