File size: 999 Bytes
ff0e97f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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")