Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,66 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import random
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
if
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return collage
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# Step 2: Grid selection
|
| 66 |
-
grid_layout = gr.Radio(
|
| 67 |
-
choices=["1x2", "1x3", "2x2", "2x3"],
|
| 68 |
-
label="Select Grid Layout",
|
| 69 |
-
value="1x2" # Default value
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
# Step 3: Orientation selection for 2x2 and 2x3
|
| 73 |
-
orientation = gr.Radio(
|
| 74 |
-
choices=["Vertical", "Horizontal"],
|
| 75 |
-
label="Select Orientation",
|
| 76 |
-
value="Vertical", # Default value
|
| 77 |
-
visible=False # Initially hidden, will only show for 2x2 and 2x3
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
# Show orientation options only when grid layout is 2x2 or 2x3
|
| 81 |
-
grid_layout.change(
|
| 82 |
-
lambda layout: gr.update(visible=layout in ["2x2", "2x3"]),
|
| 83 |
-
inputs=[grid_layout],
|
| 84 |
-
outputs=[orientation]
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
# Step 4: Display preview of the grid
|
| 88 |
-
output = gr.Image(label="Collage Preview")
|
| 89 |
-
|
| 90 |
-
# Step 5: Process the images when files are uploaded or grid layout is selected
|
| 91 |
-
uploaded_files.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
|
| 92 |
-
grid_layout.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
|
| 93 |
-
orientation.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
|
| 94 |
-
|
| 95 |
-
# Step 6: Download button for the final collage
|
| 96 |
-
download_button = gr.Button("Download Collage")
|
| 97 |
-
download_button.click(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
|
| 98 |
-
|
| 99 |
-
app.launch(share=True) # Public link to share the app
|
|
|
|
|
|
|
| 1 |
import random
|
| 2 |
from PIL import Image
|
| 3 |
|
| 4 |
+
def create_collage(images, layout):
|
| 5 |
+
collage_width = 1000 # Width of the final collage
|
| 6 |
+
collage_height = 1000 # Height of the final collage
|
| 7 |
+
img_width = collage_width // 2 # Set image width based on grid size
|
| 8 |
+
img_height = collage_height // 2 # Set image height based on grid size
|
| 9 |
+
|
| 10 |
+
if layout == '1x2': # Horizontal grid with 2 images
|
| 11 |
+
grid_cols = 2 # 2 images horizontally
|
| 12 |
+
grid_rows = 1 # 1 image vertically
|
| 13 |
+
img_width = collage_width // grid_cols
|
| 14 |
+
img_height = collage_height // grid_rows
|
| 15 |
+
|
| 16 |
+
collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
|
| 17 |
+
|
| 18 |
+
for i in range(len(images)):
|
| 19 |
+
img = images[i]
|
| 20 |
+
img_resized = img.resize((img_width, img_height), Image.LANCZOS)
|
| 21 |
+
x_pos = (i % grid_cols) * img_width
|
| 22 |
+
y_pos = (i // grid_cols) * img_height
|
| 23 |
+
collage.paste(img_resized, (x_pos, y_pos))
|
| 24 |
+
|
| 25 |
+
elif layout == '1x3': # Horizontal grid with 3 images
|
| 26 |
+
grid_cols = 3 # 3 images horizontally
|
| 27 |
+
grid_rows = 1 # 1 image vertically
|
| 28 |
+
img_width = collage_width // grid_cols
|
| 29 |
+
img_height = collage_height // grid_rows
|
| 30 |
+
|
| 31 |
+
collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
|
| 32 |
+
|
| 33 |
+
for i in range(len(images)):
|
| 34 |
+
img = images[i]
|
| 35 |
+
img_resized = img.resize((img_width, img_height), Image.LANCZOS)
|
| 36 |
+
x_pos = (i % grid_cols) * img_width
|
| 37 |
+
y_pos = (i // grid_cols) * img_height
|
| 38 |
+
collage.paste(img_resized, (x_pos, y_pos))
|
| 39 |
+
|
| 40 |
+
elif layout == '2x2' or layout == '2x3': # Random layout with 2x2 or 2x3
|
| 41 |
+
grid_cols = 2 # 2 images horizontally
|
| 42 |
+
grid_rows = 2 if layout == '2x2' else 3 # 2 or 3 images vertically
|
| 43 |
+
img_width = collage_width // grid_cols
|
| 44 |
+
img_height = collage_height // grid_rows
|
| 45 |
+
|
| 46 |
+
collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
|
| 47 |
+
|
| 48 |
+
# Shuffle images randomly
|
| 49 |
+
random.shuffle(images)
|
| 50 |
+
|
| 51 |
+
for i in range(len(images)):
|
| 52 |
+
img = images[i]
|
| 53 |
+
img_resized = img.resize((img_width, img_height), Image.LANCZOS)
|
| 54 |
+
row = i // grid_cols
|
| 55 |
+
col = i % grid_cols
|
| 56 |
+
x_pos = col * img_width
|
| 57 |
+
y_pos = row * img_height
|
| 58 |
+
collage.paste(img_resized, (x_pos, y_pos))
|
| 59 |
|
| 60 |
return collage
|
| 61 |
|
| 62 |
+
# Example usage
|
| 63 |
+
# Assuming 'images' is a list of loaded PIL Image objects
|
| 64 |
+
layout = '2x3' # or '1x2', '1x3', '2x2'
|
| 65 |
+
collage = create_collage(images, layout)
|
| 66 |
+
collage.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|