-
Notifications
You must be signed in to change notification settings - Fork 0
Add image optimization script #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9893b4a
Add image optimization script
mrbiggred bae3ba2
Add post for April 14, 2026: What are you Working On? with featured i…
mrbiggred f471272
Remove .webp from supported extensions in optimize_image.py
mrbiggred 18b0e10
Add Python 3.14 minimum version requirement
mrbiggred 0c6fcc4
Remove unneeded __future__ annotations import
mrbiggred 947b846
Fix CRLF line endings in script and add .gitattributes
mrbiggred 1b171a4
Apply EXIF orientation before resizing in optimize_image.py
mrbiggred 6c781be
Add argparse validation for --quality and --max-width
mrbiggred 14be4f5
Fix requirements-dev headers to reflect uv pip compile tooling
mrbiggred File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Ensure consistent line endings | ||
| * text=auto | ||
|
|
||
| # Scripts must use LF | ||
| *.py text eol=lf | ||
| *.sh text eol=lf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 3.14 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --- | ||
| title: "What are you Working On?" | ||
| date: 2026-04-14 | ||
| authors: | ||
| - chris | ||
| categories: | ||
| - Community | ||
| tags: | ||
| - open-discussion | ||
| - side-projects | ||
| - coding-practices | ||
| --- | ||
|
|
||
| Today's chat (2026-04-14) is an opportunity to show what you have been working. Give us a quick explanation, and if time permits, a quick demo. By quick I mean 5 minutes or less. The actual time will be driven by how many people want to show off. | ||
|
|
||
| Anything software or technical is allowed. Some examples: | ||
|
|
||
| - a product you are working on | ||
| - some cool code you wrote | ||
| - an interesting problem you recently solved | ||
|
|
||
| Looking forward to hearing what everyone has been working on lately! | ||
|
|
||
| Everyone and anyone are welcome to [join](https://weeklydevchat.com/join/) as long as you are kind, supportive, and respectful of others. Zoom link will be posted at 12pm MDT. | ||
|
|
||
| *Featured image was created with Nano Banana using this post as the prompt. It is both amazing and creepy at the same time. | ||
|
|
||
|  |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Dev-only dependencies (not needed for production builds) | ||
| # Regenerate with: uv pip compile requirements-dev.in --output-file requirements-dev.txt | ||
|
|
||
| Pillow>=10.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # This file was autogenerated by uv via the following command: | ||
| # uv pip compile requirements-dev.in --output-file requirements-dev.txt | ||
| pillow==11.3.0 | ||
| # via -r requirements-dev.in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Optimize images for the Weekly Dev Chat website. | ||
|
|
||
| Converts images to WebP format, resizes to a max width, and reports savings. | ||
| The original file is preserved; the optimized WebP is written alongside it. | ||
|
|
||
| Usage: | ||
| python scripts/optimize_image.py image1.png image2.jpg | ||
| python scripts/optimize_image.py --quality 85 --max-width 1600 image.png | ||
| """ | ||
|
|
||
| import argparse | ||
| import sys | ||
|
|
||
| if sys.version_info < (3, 14): | ||
| print( | ||
| f"Error: Python 3.14 or later is required (running {sys.version}).", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| try: | ||
| from PIL import Image, ImageOps | ||
| except ImportError: | ||
| print( | ||
| "Error: Pillow is not installed.\n" | ||
| "Install dev dependencies with:\n" | ||
| " pip install -r requirements-dev.txt\n" | ||
| "Or install Pillow directly:\n" | ||
| " pip install Pillow", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| SUPPORTED_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".tif"} | ||
|
|
||
|
|
||
| def _quality_int(value: str) -> int: | ||
| """Argparse type for --quality: integer in range 1–100.""" | ||
| v = int(value) | ||
| if not (1 <= v <= 100): | ||
| raise argparse.ArgumentTypeError(f"quality must be between 1 and 100 (got {v})") | ||
| return v | ||
|
|
||
|
|
||
| def _positive_int(value: str) -> int: | ||
| """Argparse type for --max-width: integer greater than 0.""" | ||
| v = int(value) | ||
| if v <= 0: | ||
| raise argparse.ArgumentTypeError(f"max-width must be greater than 0 (got {v})") | ||
| return v | ||
|
|
||
|
|
||
| def optimize_image(input_path: Path, *, quality: int, max_width: int) -> Path | None: | ||
| """Optimize a single image. Returns the output path, or None on error.""" | ||
| if input_path.suffix.lower() not in SUPPORTED_EXTENSIONS: | ||
| print(f" Skipping {input_path.name}: unsupported format") | ||
| return None | ||
|
|
||
| try: | ||
| img = Image.open(input_path) | ||
|
normanlorrain marked this conversation as resolved.
|
||
| except Exception as e: | ||
| print(f" Error opening {input_path.name}: {e}", file=sys.stderr) | ||
| return None | ||
|
|
||
| # Apply EXIF orientation so JPEGs rotated via metadata are correctly oriented | ||
| img = ImageOps.exif_transpose(img) | ||
|
|
||
| # Convert palette/RGBA images appropriately for WebP | ||
| if img.mode in ("P", "PA"): | ||
| img = img.convert("RGBA") | ||
| elif img.mode not in ("RGB", "RGBA"): | ||
| img = img.convert("RGB") | ||
|
|
||
| # Resize if wider than max_width, preserving aspect ratio | ||
| if img.width > max_width: | ||
| ratio = max_width / img.width | ||
| new_height = round(img.height * ratio) | ||
| img = img.resize((max_width, new_height), Image.LANCZOS) | ||
|
mrbiggred marked this conversation as resolved.
|
||
|
|
||
| output_path = input_path.with_suffix(".webp") | ||
| img.save(output_path, "WEBP", quality=quality) | ||
|
|
||
| original_size = input_path.stat().st_size | ||
| optimized_size = output_path.stat().st_size | ||
| change_pct = (optimized_size / original_size - 1) * 100 if original_size > 0 else 0 | ||
|
mrbiggred marked this conversation as resolved.
|
||
|
|
||
| print(f" {input_path.name}") | ||
| print(f" {original_size:,} bytes → {optimized_size:,} bytes ({change_pct:+.1f}%)") | ||
| print(f" Saved to {output_path.name} ({img.width}x{img.height})") | ||
|
|
||
| return output_path | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description="Optimize images for the Weekly Dev Chat website.", | ||
| ) | ||
| parser.add_argument( | ||
| "images", | ||
| nargs="+", | ||
| type=Path, | ||
| help="One or more image file paths to optimize.", | ||
| ) | ||
| parser.add_argument( | ||
| "--quality", | ||
| type=_quality_int, | ||
| default=80, | ||
| help="WebP quality (1-100, default: 80).", | ||
| ) | ||
| parser.add_argument( | ||
| "--max-width", | ||
| type=_positive_int, | ||
| default=1200, | ||
| help="Max image width in pixels (default: 1200). Images smaller than this are not upscaled.", | ||
| ) | ||
|
mrbiggred marked this conversation as resolved.
|
||
| args = parser.parse_args() | ||
|
|
||
| successes = 0 | ||
| failures = 0 | ||
|
|
||
| for image_path in args.images: | ||
| if not image_path.is_file(): | ||
| print(f" Warning: {image_path} not found, skipping.", file=sys.stderr) | ||
| failures += 1 | ||
| continue | ||
|
|
||
| result = optimize_image(image_path, quality=args.quality, max_width=args.max_width) | ||
| if result: | ||
| successes += 1 | ||
| else: | ||
| failures += 1 | ||
|
|
||
| print(f"\nDone: {successes} optimized, {failures} skipped/failed.") | ||
| sys.exit(1 if failures > 0 and successes == 0 else 0) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.