stingdressup / functions.py
chips
asyncing
0d060b5
raw
history blame
1.55 kB
from PIL import Image
import os
from io import BytesIO
import base64
import uuid
async def combine_images_side_by_side(image1: bytes, image2: bytes) -> str:
"""
Takes two images, scales them to 1280x1280, combines them side by side,
saves to tmp storage, and returns the base64 encoded image string.
Args:
image1: First image as bytes
image2: Second image as bytes
Returns:
Base64 encoded string of the combined image
"""
# Create temp directory if it doesn't exist
os.makedirs('tmp', exist_ok=True)
# Open images from bytes
img1 = Image.open(BytesIO(image1))
img2 = Image.open(BytesIO(image2))
# Scale both images to 1280x1280
img1 = img1.resize((1280, 1280))
img2 = img2.resize((1280, 1280))
# Create new image with combined width (2560) and height (1280)
combined = Image.new('RGB', (2560, 1280))
# Paste images side by side
combined.paste(img1, (0, 0))
combined.paste(img2, (1280, 0))
# Generate unique filename
filename = f"tmp/combined_{uuid.uuid4()}.png"
# Save image
combined.save(filename)
# Convert to base64
with open(filename, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
# Clean up
os.remove(filename)
return encoded_string
# Example usage:
# with open('image1.jpg', 'rb') as f1, open('image2.jpg', 'rb') as f2:
# combined_image = combine_images_side_by_side(f1.read(), f2.read())