How do you apply the same transformation to every document page?
all_frames can apply a function as it extracts each frame and returns the resulting images as a list.
Pixel experiment and interpretation
Enlarge both scanned pages and check the resulting dimensions and count. Holding every transformed page at once uses more memory on long documents.
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)
out=ImageSequence.all_frames(pages(),lambda f:f.resize((2,3)))
[f.size for f in out]Result [(2, 3), (2, 3)]
中文
怎样对多页文档的每页执行同一缩放操作?
all_frames 可在提取各帧时应用函数,返回处理后的图像列表。
像素实验与适用边界
将双页扫描统一放大,再确认页数与尺寸。长文档一次保存全部结果会占用更多内存。
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)
out=ImageSequence.all_frames(pages(),lambda f:f.resize((2,3)))
[f.size for f in out]结果 [(2, 3), (2, 3)]