import gr.Interface as gr
from PIL import Image
def merge_images(images):
# Initialize a 3x3 grid with empty blocks (16x16 white images)
grid_size = (3, 3)
image_size = (256, 256) # Define the size for each image
empty_block = Image.new("RGB", (16, 16), (255, 255, 255))
images = images + [empty_block] * (9-len(images))
# Create a blank canvas for the final 3x3 grid
grid_image = Image.new("RGB", (image_size[0]*grid_size[0], image_size[1]*grid_size[1]), (255, 255, 255))
for idx, img in enumerate(images):
row, col = divmod(idx, 3)
# Resize each image to fit 256x256, or use the empty block directly
if img != empty_block:
img = img.resize(image_size)
else:
img = empty_block
# Paste the image into the grid
grid_image.paste(img, (col * image_size[0], row * image_size[1]))
return grid_image
# Create a Gradio interface
iface = gr.Interface(
fn=merge_images,
inputs=[gr.inputs.Image(type="pil", shape=(256, 256), optional=True, label=f"Image {i+1}") for i in range(9)],
outputs="image",
title="3x3 Image Grid",
description="Upload up to 9 images to merge them into a 3x3 grid. Missing images will be replaced by a 16x16 white block."
)
iface.launch()