图片缩略图#

您可以使用 Matplotlib 从现有图像生成缩略图。Matplotlib 依赖Pillow读取图像,因此支持 Pillow 支持的所有格式。

from argparse import ArgumentParser
from pathlib import Path
import sys
import matplotlib.image as image


parser = ArgumentParser(
    description="Build thumbnails of all images in a directory.")
parser.add_argument("imagedir", type=Path)
args = parser.parse_args()
if not args.imagedir.is_dir():
    sys.exit(f"Could not find input directory {args.imagedir}")

outdir = Path("thumbs")
outdir.mkdir(parents=True, exist_ok=True)

for path in args.imagedir.glob("*.png"):
    outpath = outdir / path.name
    fig = image.thumbnail(path, outpath, scale=0.15)
    print(f"saved thumbnail of {path} to {outpath}")

由 Sphinx-Gallery 生成的画廊