Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
# Function to
|
| 5 |
-
def
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
choices=["1x2", "1x3", "2x2", "2x3"],
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
# Function to process and display the images in grid format
|
| 5 |
+
def create_collage(uploaded_files, grid_layout):
|
| 6 |
+
# Open images
|
| 7 |
+
images = [Image.open(file.name) for file in uploaded_files]
|
| 8 |
+
|
| 9 |
+
# Determine grid dimensions based on layout
|
| 10 |
+
if grid_layout == "1x2":
|
| 11 |
+
rows, cols = 1, 2
|
| 12 |
+
elif grid_layout == "1x3":
|
| 13 |
+
rows, cols = 1, 3
|
| 14 |
+
elif grid_layout == "2x2":
|
| 15 |
+
rows, cols = 2, 2
|
| 16 |
+
elif grid_layout == "2x3":
|
| 17 |
+
rows, cols = 2, 3
|
| 18 |
+
|
| 19 |
+
# Create a blank canvas for the collage without spacing
|
| 20 |
+
collage_width = max(img.width for img in images) * cols
|
| 21 |
+
collage_height = max(img.height for img in images) * rows
|
| 22 |
+
collage = Image.new("RGB", (collage_width, collage_height))
|
| 23 |
+
|
| 24 |
+
# Resize images to fit the grid
|
| 25 |
+
resized_images = []
|
| 26 |
+
for img in images:
|
| 27 |
+
# Resize or crop image to fit the grid cell size
|
| 28 |
+
img_width = collage_width // cols
|
| 29 |
+
img_height = collage_height // rows
|
| 30 |
+
img_resized = img.resize((img_width, img_height), Image.ANTIALIAS) # Resize to fit
|
| 31 |
+
resized_images.append(img_resized)
|
| 32 |
+
|
| 33 |
+
# Place images on the canvas in grid format without spacing
|
| 34 |
+
for i, img in enumerate(resized_images):
|
| 35 |
+
row = i // cols
|
| 36 |
+
col = i % cols
|
| 37 |
+
collage.paste(img, (col * img.width, row * img.height))
|
| 38 |
+
|
| 39 |
+
return collage
|
| 40 |
+
|
| 41 |
+
# Gradio Interface
|
| 42 |
+
with gr.Blocks() as app:
|
| 43 |
+
with gr.Column():
|
| 44 |
+
# Step 1: File upload for multiple images
|
| 45 |
+
uploaded_files = gr.Files(label="Upload Images", file_count="multiple", file_types=["image"])
|
| 46 |
+
|
| 47 |
+
# Step 2: Grid selection
|
| 48 |
+
grid_layout = gr.Radio(
|
| 49 |
choices=["1x2", "1x3", "2x2", "2x3"],
|
| 50 |
+
label="Select Grid Layout",
|
| 51 |
+
value="1x2" # Default value
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Step 3: Display preview of the grid
|
| 55 |
+
output = gr.Image(label="Collage Preview")
|
| 56 |
+
|
| 57 |
+
# Step 4: Process the images when files are uploaded or grid layout is selected
|
| 58 |
+
uploaded_files.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
|
| 59 |
+
grid_layout.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
|
| 60 |
+
|
| 61 |
+
# Step 5: Download button for the final collage
|
| 62 |
+
download_button = gr.Button("Download Collage")
|
| 63 |
+
download_button.click(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
|
| 64 |
+
|
| 65 |
+
app.launch()
|