| import base64 | |
| from PIL import Image | |
| from io import BytesIO | |
| # Load and optimize image | |
| image = Image.open("examples/blue_jay.jpg") | |
| # Resize if too large (max 800px on longest side) | |
| max_size = 800 | |
| if max(image.size) > max_size: | |
| ratio = max_size / max(image.size) | |
| new_size = tuple(int(dim * ratio) for dim in image.size) | |
| image = image.resize(new_size, Image.Resampling.LANCZOS) | |
| # Convert to JPEG with compression | |
| buffer = BytesIO() | |
| image.convert("RGB").save(buffer, format="JPEG", quality=85, optimize=True) | |
| image_bytes = buffer.getvalue() | |
| # Encode to base64 | |
| image_base64 = base64.b64encode(image_bytes).decode() | |
| # Print full base64 for copying | |
| print("Full base64 string:") | |
| print(image_base64) | |
| # Also save to file for easy copying | |
| with open("examples/blue_jay_base64.txt", "w") as out: | |
| out.write(image_base64) | |
| print(f"\nβ Saved to examples/blue_jay_base64.txt") | |
| print(f"π Size: {len(image_base64)} characters") | |
| print(f"π¦ Optimized payload: ~{len(image_base64) // 1024}KB") |