Update app.py
Browse files1234567891011121314151617181920212223242526272829303132import gradio as grimport os# Function to handle image upload and layout settingdef process_images(layout, image_files):# Save uploaded imagesimg_paths = []for img_file in image_files:img_path = f"temp_images/{img_file.name}"img_file.save(img_path)img_paths.append(img_path)# Return the layout and image paths to the front-end (HTML and JS will take care of it)return layout, img_paths# Gradio Interface setupiface = gr.Interface(fn=process_images,inputs=[gr.CheckboxGroup(label="Choose Grid Layout",choices=["1x2", "1x3", "2x2", "2x3"],type="value" # This allows for the selection of multiple options if needed),gr.File(label="Upload Images", type="file", multiple=True) # Multiple image upload],outputs=["text", "json"], # We'll output a layout and list of image pathslive=True # Updates in real-time)# Run Gradio appiface.launch(share=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import gradio as grfrom PIL import Imageimport io# Function to process and display the images in grid formatdef create_collage(uploaded_files, grid_layout):# Open imagesimages = [Image.open(file.name) for file in uploaded_files]# Determine grid dimensions based on layoutif grid_layout == "1x2":rows, cols = 1, 2elif grid_layout == "1x3":rows, cols = 1, 3elif grid_layout == "2x2":rows, cols = 2, 2elif grid_layout == "2x3":rows, cols = 2, 3# Create a blank canvas for the collagecollage_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))# Place images on the canvas in grid formatfor i, img in enumerate(images):row = i // colscol = i % colscollage.paste(img, (col * img.width, row * img.height))return 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: Display preview of the gridoutput = gr.Image(label="Collage Preview")# Step 4: Process the images when files are uploaded or grid layout is selecteduploaded_files.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)grid_layout.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)# Step 5: Download button for the final collagedownload_button = gr.Button("Download Collage")download_button.click(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)app.launch()
app.py+55-30