|
|
import gradio as gr |
|
|
from PIL import Image |
|
|
import os |
|
|
|
|
|
|
|
|
def create_collage(uploaded_files, grid_layout): |
|
|
|
|
|
images = [Image.open(file.name) for file in uploaded_files] |
|
|
|
|
|
|
|
|
if grid_layout == "1x2": |
|
|
rows, cols = 1, 2 |
|
|
elif grid_layout == "1x3": |
|
|
rows, cols = 1, 3 |
|
|
elif grid_layout == "2x2": |
|
|
rows, cols = 2, 2 |
|
|
elif grid_layout == "2x3": |
|
|
rows, cols = 2, 3 |
|
|
|
|
|
|
|
|
collage_width = max(img.width for img in images) * cols |
|
|
collage_height = max(img.height for img in images) * rows |
|
|
collage = Image.new("RGB", (collage_width, collage_height)) |
|
|
|
|
|
|
|
|
resized_images = [] |
|
|
for img in images: |
|
|
img_width = collage_width // cols |
|
|
img_height = collage_height // rows |
|
|
img_resized = img.resize((img_width, img_height), Image.Resampling.LANCZOS) |
|
|
resized_images.append(img_resized) |
|
|
|
|
|
|
|
|
for i, img in enumerate(resized_images): |
|
|
row = i // cols |
|
|
col = i % cols |
|
|
collage.paste(img, (col * img.width, row * img.height)) |
|
|
|
|
|
|
|
|
output_path = "temp_collage.png" |
|
|
collage.save(output_path, format="PNG") |
|
|
|
|
|
|
|
|
return output_path |
|
|
|
|
|
|
|
|
with gr.Blocks() as app: |
|
|
with gr.Column(): |
|
|
|
|
|
gr.Markdown("# **Image Collage Maker**") |
|
|
gr.Markdown("Create custom image collages with different grid layouts like 1x2, 1x3, 2x2, and 2x3. Upload your images and select a layout!") |
|
|
|
|
|
|
|
|
uploaded_files = gr.Files(label="Upload Images", file_count="multiple", file_types=["image"]) |
|
|
|
|
|
|
|
|
grid_layout = gr.Radio( |
|
|
choices=["1x2", "1x3", "2x2", "2x3"], |
|
|
label="Select Grid Layout", |
|
|
value="1x2" |
|
|
) |
|
|
|
|
|
|
|
|
output = gr.Image(label="Collage Preview") |
|
|
|
|
|
|
|
|
uploaded_files.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output) |
|
|
grid_layout.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output) |
|
|
|
|
|
|
|
|
download_button = gr.Button("Download Collage (PNG)") |
|
|
download_button.click(create_collage, inputs=[uploaded_files, grid_layout], outputs=gr.File(label="Download Collage")) |
|
|
|
|
|
app.launch(share=True) |
|
|
|