|
| 1 | +# Contributing to docs |
| 2 | + |
| 3 | +This document provides guidelines for contributing to the documentation. |
| 4 | + |
| 5 | +## Images |
| 6 | + |
| 7 | +When you include images, use the `resize.py` script to resize them to a width of 720 pixels and save them as compressed JPEGs. This ensures that images are optimized for web use. This helps reduce page load times and reduce total repo size. |
| 8 | + |
| 9 | +```python |
| 10 | +from PIL import Image |
| 11 | +import sys |
| 12 | +import os |
| 13 | + |
| 14 | +def resize_image(input_path, output_path=None, width=720, quality=80): |
| 15 | + """ |
| 16 | + Resize an image to the specified width while maintaining aspect ratio, |
| 17 | + then save it as a compressed JPEG for web use. |
| 18 | + """ |
| 19 | + img = Image.open(input_path) |
| 20 | + w_percent = width / float(img.width) |
| 21 | + height = int(img.height * w_percent) |
| 22 | + |
| 23 | + resized = img.resize((width, height), Image.LANCZOS) |
| 24 | + if resized.mode in ("RGBA", "P"): |
| 25 | + resized = resized.convert("RGB") |
| 26 | + |
| 27 | + if output_path is None: |
| 28 | + base, _ = os.path.splitext(input_path) |
| 29 | + output_path = f"{base}_resized.jpg" |
| 30 | + |
| 31 | + resized.save(output_path, "JPEG", quality=quality, optimize=True) |
| 32 | + print(f"Saved: {output_path} ({width}×{height}, quality={quality}%)") |
| 33 | + |
| 34 | +if __name__ == "__main__": |
| 35 | + if len(sys.argv) < 2: |
| 36 | + print("Usage: python resize.py <input_path> [output_path]") |
| 37 | + sys.exit(1) |
| 38 | + inp = sys.argv[1] |
| 39 | + outp = sys.argv[2] if len(sys.argv) > 2 else None |
| 40 | + resize_image(inp, outp) |
| 41 | +``` |
| 42 | + |
| 43 | +You can easily resize images in bulk using a bash script: |
| 44 | + |
| 45 | +```bash |
| 46 | +for img in *.png *.jpg; do |
| 47 | + python resize.py "$img" |
| 48 | +done |
| 49 | +``` |
0 commit comments