29 lines
817 B
Python
29 lines
817 B
Python
import numpy as np
|
|
from PIL import Image
|
|
from io import BytesIO
|
|
import base64
|
|
|
|
def decode_and_resize(image_string):
|
|
img_data = base64.b64decode(image_string)
|
|
img = Image.open(BytesIO(img_data))
|
|
return img.resize((512, 512))
|
|
|
|
def combine_images(images):
|
|
combined = Image.new('RGB', (1536, 1536)) # 3 * 512 = 1536
|
|
|
|
for i, img in enumerate(images):
|
|
x = i % 3 * 512
|
|
y = i // 3 * 512
|
|
combined.paste(img, (x, y))
|
|
|
|
return combined
|
|
|
|
def create_grid(b64_images):
|
|
# decode, resize, and combine images
|
|
images = [decode_and_resize(img_str) for img_str in b64_images]
|
|
combined = combine_images(images)
|
|
|
|
# convert combined image to byte stream for posting
|
|
img_byte_arr = BytesIO()
|
|
combined.save(img_byte_arr, format='WEBP')
|
|
return img_byte_arr.getvalue() |