Note
Go to the endto download the full example code.
Image thumbnail#
You can use Matplotlib to generate thumbnails from existing images.Matplotlib relies onPillow for reading images, and thus supports all formatssupported by Pillow.
fromargparseimportArgumentParserfrompathlibimportPathimportsysimportmatplotlib.imageasimageparser=ArgumentParser(description="Build thumbnails of all images in a directory.")parser.add_argument("imagedir",type=Path)args=parser.parse_args()ifnotargs.imagedir.is_dir():sys.exit(f"Could not find input directory{args.imagedir}")outdir=Path("thumbs")outdir.mkdir(parents=True,exist_ok=True)forpathinargs.imagedir.glob("*.png"):outpath=outdir/path.namefig=image.thumbnail(path,outpath,scale=0.15)print(f"saved thumbnail of{path} to{outpath}")