askrobots / dbbasic-video
Public
Video analysis: download, keyframes, transcripts, thumbnail grids and AI summaries of what's on screen.
make_grid.py · 2.1 KB · 69 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Create a thumbnail sheet from all keyframes |
| 4 | """ |
| 5 | |
| 6 | import math |
| 7 | from pathlib import Path |
| 8 | from PIL import Image |
| 9 | |
| 10 | |
| 11 | def create_thumbnail_sheet(keyframes_dir, output_file="thumbnail_sheet.jpg", thumb_width=160): |
| 12 | """Create a grid of thumbnails from all keyframes.""" |
| 13 | keyframes_path = Path(keyframes_dir) |
| 14 | image_files = sorted(keyframes_path.glob("scene_*.jpg")) |
| 15 | |
| 16 | if not image_files: |
| 17 | print(f"No keyframes found in {keyframes_dir}") |
| 18 | return |
| 19 | |
| 20 | print(f"Creating thumbnail sheet from {len(image_files)} keyframes...") |
| 21 | |
| 22 | # Calculate grid dimensions (roughly square) |
| 23 | num_images = len(image_files) |
| 24 | cols = math.ceil(math.sqrt(num_images)) |
| 25 | rows = math.ceil(num_images / cols) |
| 26 | |
| 27 | # Load first image to get aspect ratio |
| 28 | first_img = Image.open(image_files[0]) |
| 29 | aspect_ratio = first_img.height / first_img.width |
| 30 | thumb_height = int(thumb_width * aspect_ratio) |
| 31 | first_img.close() |
| 32 | |
| 33 | # Create blank canvas |
| 34 | canvas_width = cols * thumb_width |
| 35 | canvas_height = rows * thumb_height |
| 36 | canvas = Image.new('RGB', (canvas_width, canvas_height), (0, 0, 0)) |
| 37 | |
| 38 | # Place thumbnails |
| 39 | for idx, img_path in enumerate(image_files): |
| 40 | row = idx // cols |
| 41 | col = idx % cols |
| 42 | |
| 43 | img = Image.open(img_path) |
| 44 | img.thumbnail((thumb_width, thumb_height), Image.Resampling.LANCZOS) |
| 45 | |
| 46 | x = col * thumb_width |
| 47 | y = row * thumb_height |
| 48 | canvas.paste(img, (x, y)) |
| 49 | img.close() |
| 50 | |
| 51 | if (idx + 1) % 20 == 0: |
| 52 | print(f" Processed {idx + 1}/{num_images}...") |
| 53 | |
| 54 | # Save |
| 55 | canvas.save(output_file, "JPEG", quality=85, optimize=True) |
| 56 | print(f"\nThumbnail sheet saved to {output_file}") |
| 57 | print(f"Grid: {cols} columns × {rows} rows") |
| 58 | print(f"Thumbnail size: {thumb_width}×{thumb_height}px") |
| 59 | print(f"Total size: {canvas_width}×{canvas_height}px") |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | import sys |
| 64 | |
| 65 | keyframes_dir = sys.argv[1] if len(sys.argv) > 1 else "keyframes" |
| 66 | output_file = sys.argv[2] if len(sys.argv) > 2 else "thumbnail_sheet.jpg" |
| 67 | thumb_width = int(sys.argv[3]) if len(sys.argv) > 3 else 160 |
| 68 | |
| 69 | create_thumbnail_sheet(keyframes_dir, output_file, thumb_width) |