BirdScopeAI / examples /b64_helper.py
facemelter's picture
Initial commit to hf space for hackathon
ff0e97f verified
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")