cr8 commited on
Commit
5b3c103
·
verified ·
1 Parent(s): 061f5e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -30
app.py CHANGED
@@ -1,32 +1,57 @@
1
  import gradio as gr
2
- import os
3
-
4
- # Function to handle image upload and layout setting
5
- def process_images(layout, image_files):
6
- # Save uploaded images
7
- img_paths = []
8
- for img_file in image_files:
9
- img_path = f"temp_images/{img_file.name}"
10
- img_file.save(img_path)
11
- img_paths.append(img_path)
12
-
13
- # Return the layout and image paths to the front-end (HTML and JS will take care of it)
14
- return layout, img_paths
15
-
16
- # Gradio Interface setup
17
- iface = gr.Interface(
18
- fn=process_images,
19
- inputs=[
20
- gr.CheckboxGroup(
21
- label="Choose Grid Layout",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  choices=["1x2", "1x3", "2x2", "2x3"],
23
- type="value" # This allows for the selection of multiple options if needed
24
- ),
25
- gr.File(label="Upload Images", type="file", multiple=True) # Multiple image upload
26
- ],
27
- outputs=["text", "json"], # We'll output a layout and list of image paths
28
- live=True # Updates in real-time
29
- )
30
-
31
- # Run Gradio app
32
- iface.launch(share=True)
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ import io
4
+
5
+ # Function to process and display the images in grid format
6
+ def create_collage(uploaded_files, grid_layout):
7
+ # Open images
8
+ images = [Image.open(file.name) for file in uploaded_files]
9
+
10
+ # Determine grid dimensions based on layout
11
+ if grid_layout == "1x2":
12
+ rows, cols = 1, 2
13
+ elif grid_layout == "1x3":
14
+ rows, cols = 1, 3
15
+ elif grid_layout == "2x2":
16
+ rows, cols = 2, 2
17
+ elif grid_layout == "2x3":
18
+ rows, cols = 2, 3
19
+
20
+ # Create a blank canvas for the collage
21
+ collage_width = max(img.width for img in images) * cols
22
+ collage_height = max(img.height for img in images) * rows
23
+ collage = Image.new("RGB", (collage_width, collage_height))
24
+
25
+ # Place images on the canvas in grid format
26
+ for i, img in enumerate(images):
27
+ row = i // cols
28
+ col = i % cols
29
+ collage.paste(img, (col * img.width, row * img.height))
30
+
31
+ return collage
32
+
33
+ # Gradio Interface
34
+ with gr.Blocks() as app:
35
+ with gr.Column():
36
+ # Step 1: File upload for multiple images
37
+ uploaded_files = gr.Files(label="Upload Images", file_count="multiple", file_types=["image"])
38
+
39
+ # Step 2: Grid selection
40
+ grid_layout = gr.Radio(
41
  choices=["1x2", "1x3", "2x2", "2x3"],
42
+ label="Select Grid Layout",
43
+ value="1x2" # Default value
44
+ )
45
+
46
+ # Step 3: Display preview of the grid
47
+ output = gr.Image(label="Collage Preview")
48
+
49
+ # Step 4: Process the images when files are uploaded or grid layout is selected
50
+ uploaded_files.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
51
+ grid_layout.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
52
+
53
+ # Step 5: Download button for the final collage
54
+ download_button = gr.Button("Download Collage")
55
+ download_button.click(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
56
+
57
+ app.launch()