|
| 1 | +""" |
| 2 | +Create slides gif thumbnails to beautify the README.md |
| 3 | +""" |
| 4 | +import argparse |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def main(args: argparse.Namespace): |
| 10 | + |
| 11 | + # List all pdf files (avoiding images and 4:3 aspect ratio) |
| 12 | + slides_pdf = [f for f in args.root_dir.glob('**/*.pdf')] |
| 13 | + slides_pdf = [f for f in slides_pdf |
| 14 | + if 'img' not in str(f) and '43.pdf' not in str(f)] |
| 15 | + |
| 16 | + # Extract thumbnails from pdf |
| 17 | + for slide_f in slides_pdf: |
| 18 | + print(f'Creating thumbnail for {str(slide_f)}...') |
| 19 | + cmd = ['convert', |
| 20 | + str(slide_f), |
| 21 | + '-thumbnail', |
| 22 | + f'x{args.thumb_size}', |
| 23 | + f'{args.out_dir}/frame%03d.png'] |
| 24 | + subprocess.run(cmd) |
| 25 | + |
| 26 | + # Create gif from thumbnails |
| 27 | + out_gif = args.out_dir / f'{slide_f.stem.lower()}.gif' |
| 28 | + thumb_files = sorted([str(f) for f in args.out_dir.glob('*.png')]) |
| 29 | + cmd = ['convert', |
| 30 | + '-loop', |
| 31 | + '0', |
| 32 | + '-delay', |
| 33 | + '100', |
| 34 | + *thumb_files, |
| 35 | + str(out_gif)] |
| 36 | + subprocess.run(cmd) |
| 37 | + |
| 38 | + # Cleanup - remove all temporary `frame*.png` files |
| 39 | + for thumb_f in thumb_files: |
| 40 | + Path(thumb_f).unlink() |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == '__main__': |
| 44 | + parser = argparse.ArgumentParser() |
| 45 | + parser.add_argument('--root_dir', type=Path, default='.') |
| 46 | + parser.add_argument('--out_dir', type=Path, default='./thumbs') |
| 47 | + parser.add_argument('--thumb_size', type=int, default=256) |
| 48 | + args = parser.parse_args() |
| 49 | + |
| 50 | + if not args.out_dir.is_dir(): |
| 51 | + args.out_dir.mkdir(exist_ok=True, parents=True) |
| 52 | + |
| 53 | + main(args) |
0 commit comments