Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import 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 imagesimages = [Image.open(file.name) for file in uploaded_files]# Define basic grid layoutif grid_layout == "1x2":rows, cols = 1, 2elif grid_layout == "1x3":rows, cols = 1, 3elif grid_layout == "2x2":if orientation == "Vertical":rows, cols = 2, 2 # Vertical layoutelse:rows, cols = 1, 4 # Horizontal layout (single row)elif grid_layout == "2x3":if orientation == "Vertical":rows, cols = 2, 3 # Vertical layoutelse:rows, cols = 1, 6 # Horizontal layout (single row)# Create a blank canvas for the collage without spacingcollage_width = max(img.width for img in images) * colscollage_height = max(img.height for img in images) * rowscollage = Image.new("RGB", (collage_width, collage_height))# Resize or randomly fit imagesresized_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 fitresized_images.append(img_resized)# Randomly place images on the canvas (imitating a comic strip)x_offset = 0y_offset = 0for i, img in enumerate(resized_images):row = i // colscol = i % cols# Ensure the random range is valid by checking image dimensionsx_pos = random.randint(x_offset, collage_width - img.width) if collage_width - img.width > x_offset else x_offsety_pos = random.randint(y_offset, collage_height - img.height) if collage_height - img.height > y_offset else y_offsetcollage.paste(img, (x_pos, y_pos))# Update offsets for next image placementx_offset = x_pos + img.widthy_offset = y_pos + img.heightreturn collage# Gradio Interfacewith gr.Blocks() as app:with gr.Column():# Step 1: File upload for multiple imagesuploaded_files = gr.Files(label="Upload Images", file_count="multiple", file_types=["image"])# Step 2: Grid selectiongrid_layout = gr.Radio(choices=["1x2", "1x3", "2x2", "2x3"],label="Select Grid Layout",value="1x2" # Default value)# Step 3: Orientation selection for 2x2 and 2x3orientation = gr.Radio(choices=["Vertical", "Horizontal"],label="Select Orientation",value="Vertical", # Default valuevisible=False # Initially hidden, will only show for 2x2 and 2x3)# Show orientation options only when grid layout is 2x2 or 2x3grid_layout.change(lambda layout: gr.update(visible=layout in ["2x2", "2x3"]),inputs=[grid_layout],outputs=[orientation])# Step 4: Display preview of the gridoutput = gr.Image(label="Collage Preview")# Step 5: Process the images when files are uploaded or grid layout is selecteduploaded_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 collagedownload_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 app123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import randomfrom PIL import Imagedef create_collage(images, layout):collage_width = 1000 # Width of the final collagecollage_height = 1000 # Height of the final collageimg_width = collage_width // 2 # Set image width based on grid sizeimg_height = collage_height // 2 # Set image height based on grid sizeif layout == '1x2': # Horizontal grid with 2 imagesgrid_cols = 2 # 2 images horizontallygrid_rows = 1 # 1 image verticallyimg_width = collage_width // grid_colsimg_height = collage_height // grid_rowscollage = 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_widthy_pos = (i // grid_cols) * img_heightcollage.paste(img_resized, (x_pos, y_pos))elif layout == '1x3': # Horizontal grid with 3 imagesgrid_cols = 3 # 3 images horizontallygrid_rows = 1 # 1 image verticallyimg_width = collage_width // grid_colsimg_height = collage_height // grid_rowscollage = 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_widthy_pos = (i // grid_cols) * img_heightcollage.paste(img_resized, (x_pos, y_pos))elif layout == '2x2' or layout == '2x3': # Random layout with 2x2 or 2x3grid_cols = 2 # 2 images horizontallygrid_rows = 2 if layout == '2x2' else 3 # 2 or 3 images verticallyimg_width = collage_width // grid_colsimg_height = collage_height // grid_rowscollage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))# Shuffle images randomlyrandom.shuffle(images)for i in range(len(images)):img = images[i]img_resized = img.resize((img_width, img_height), Image.LANCZOS)row = i // grid_colscol = i % grid_colsx_pos = col * img_widthy_pos = row * img_heightcollage.paste(img_resized, (x_pos, y_pos))return collage# 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