Update app.py
Browse files112356789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354554567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606159606162636465666768697071727374757677787980818283848586878889909192939495969798996263646566import gradio as grimport randomfrom PIL import Image
# Function to process and display the images in a comic-style griddef create_collage(uploaded_files, grid_layout, orientation): # Open images images = [Image.open(file.name) for file in uploaded_files]
# Define basic grid layout if grid_layout == "1x2": rows, cols = 1, 2 elif grid_layout == "1x3": rows, cols = 1, 3 elif grid_layout == "2x2": if orientation == "Vertical": rows, cols = 2, 2 # Vertical layout else: rows, cols = 1, 4 # Horizontal layout (single row) elif grid_layout == "2x3": if orientation == "Vertical": rows, cols = 2, 3 # Vertical layout else: rows, cols = 1, 6 # Horizontal layout (single row)
# Create a blank canvas for the collage without spacing 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))
# Resize or randomly fit images resized_images = [] for img in images: # Random width and height (mimicking comic strip style) img_width = random.randint(collage_width // (cols + 2), collage_width // cols) img_height = random.randint(collage_height // (rows + 2), collage_height // rows) img_resized = img.resize((img_width, img_height), Image.Resampling.LANCZOS) # Resize to fit resized_images.append(img_resized)
# Randomly place images on the canvas (imitating a comic strip) x_offset = 0 y_offset = 0 for i, img in enumerate(resized_images): row = i // cols col = i % cols # Ensure the random range is valid by checking image dimensions x_pos = random.randint(x_offset, collage_width - img.width) if collage_width - img.width > x_offset else x_offset y_pos = random.randint(y_offset, collage_height - img.height) if collage_height - img.height > y_offset else y_offset
collage.paste(img, (x_pos, y_pos))
# Update offsets for next image placement x_offset = x_pos + img.width y_offset = y_pos + img.heightdef create_collage(images, layout): collage_width = 1000 # Width of the final collage collage_height = 1000 # Height of the final collage img_width = collage_width // 2 # Set image width based on grid size img_height = collage_height // 2 # Set image height based on grid size
if layout == '1x2': # Horizontal grid with 2 images grid_cols = 2 # 2 images horizontally grid_rows = 1 # 1 image vertically img_width = collage_width // grid_cols img_height = collage_height // grid_rows
collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
for i in range(len(images)): img = images[i] img_resized = img.resize((img_width, img_height), Image.LANCZOS) x_pos = (i % grid_cols) * img_width y_pos = (i // grid_cols) * img_height collage.paste(img_resized, (x_pos, y_pos))
elif layout == '1x3': # Horizontal grid with 3 images grid_cols = 3 # 3 images horizontally grid_rows = 1 # 1 image vertically img_width = collage_width // grid_cols img_height = collage_height // grid_rows
collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
for i in range(len(images)): img = images[i] img_resized = img.resize((img_width, img_height), Image.LANCZOS) x_pos = (i % grid_cols) * img_width y_pos = (i // grid_cols) * img_height collage.paste(img_resized, (x_pos, y_pos))
elif layout == '2x2' or layout == '2x3': # Random layout with 2x2 or 2x3 grid_cols = 2 # 2 images horizontally grid_rows = 2 if layout == '2x2' else 3 # 2 or 3 images vertically img_width = collage_width // grid_cols img_height = collage_height // grid_rows
collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
# Shuffle images randomly random.shuffle(images)
for i in range(len(images)): img = images[i] img_resized = img.resize((img_width, img_height), Image.LANCZOS) row = i // grid_cols col = i % grid_cols x_pos = col * img_width y_pos = row * img_height collage.paste(img_resized, (x_pos, y_pos))
return collage
# Gradio Interfacewith gr.Blocks() as app: with gr.Column(): # Step 1: File upload for multiple images uploaded_files = gr.Files(label="Upload Images", file_count="multiple", file_types=["image"])
# Step 2: Grid selection grid_layout = gr.Radio( choices=["1x2", "1x3", "2x2", "2x3"], label="Select Grid Layout", value="1x2" # Default value )
# Step 3: Orientation selection for 2x2 and 2x3 orientation = gr.Radio( choices=["Vertical", "Horizontal"], label="Select Orientation", value="Vertical", # Default value visible=False # Initially hidden, will only show for 2x2 and 2x3 )
# Show orientation options only when grid layout is 2x2 or 2x3 grid_layout.change( lambda layout: gr.update(visible=layout in ["2x2", "2x3"]), inputs=[grid_layout], outputs=[orientation] )
# Step 4: Display preview of the grid output = gr.Image(label="Collage Preview")
# Step 5: Process the images when files are uploaded or grid layout is selected uploaded_files.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output) grid_layout.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output) orientation.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
# Step 6: Download button for the final collage download_button = gr.Button("Download Collage") download_button.click(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
app.launch(share=True) # Public link to share the app# Example usage# Assuming 'images' is a list of loaded PIL Image objectslayout = '2x3' # or '1x2', '1x3', '2x2'collage = create_collage(images, layout)collage.show()app.py+60-93