Why copy frames retained from a multi-page scan iterator?

Iterator walks the frames of a multi-frame image. Copy the current contents before advancing if a snapshot must remain independent.

Pixel experiment and interpretation

Think of a desk calendar with one page showing at a time. Save each snapshot first, then sample both pages after the traversal.

from PIL import Image, ImageSequence
from io import BytesIO

def pages():
    buf = BytesIO()
    first = Image.new('L', (1, 1), 20)
    second = Image.new('L', (1, 1), 80)
    first.save(buf, format='TIFF', save_all=True, append_images=[second])
    buf.seek(0)
    return Image.open(buf)

im=pages()
snapshots=[frame.copy() for frame in ImageSequence.Iterator(im)]
[f.getpixel((0,0)) for f in snapshots]

Result [20, 80]

中文

遍历多页扫描件时为什么要复制需要保留的帧?

Iterator 依次访问同一多帧图像的各帧。独立保存当前内容需要在继续遍历前复制。

像素实验与适用边界

像翻台历一样,下一页会替换眼前的一页。示例先各留一张快照,再读取两页灰度。

from PIL import Image, ImageSequence
from io import BytesIO

def pages():
    buf = BytesIO()
    first = Image.new('L', (1, 1), 20)
    second = Image.new('L', (1, 1), 80)
    first.save(buf, format='TIFF', save_all=True, append_images=[second])
    buf.seek(0)
    return Image.open(buf)

im=pages()
snapshots=[frame.copy() for frame in ImageSequence.Iterator(im)]
[f.getpixel((0,0)) for f in snapshots]

结果 [20, 80]

References / 参考来源

Keep reading / 继续阅读