How do you select a specific page of a multi-page TIFF?

seek selects a zero-based frame in a multi-frame image. Subsequent reads use that frame.

Pixel experiment and interpretation

Two pages carry readings of 20 and 80. Index one selects the second. Seeking beyond the available pages raises EOFError.

from PIL import Image
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()
im.seek(1)
(im.tell(),im.getpixel((0,0)))

Result (1, 80)

中文

如何在多页 TIFF 中读取指定页?

seek 以从零开始的帧号定位多帧图像,之后的像素读取针对当前帧。

像素实验与适用边界

两页灰度值分别为 20 和 80。选择页号 1 才读到第二页;超出文件范围会遇到 EOFError。

from PIL import Image
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()
im.seek(1)
(im.tell(),im.getpixel((0,0)))

结果 (1, 80)

References / 参考来源

Keep reading / 继续阅读