cr8 commited on
Commit
138a34e
·
verified ·
1 Parent(s): 8c319f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -30
app.py CHANGED
@@ -1,32 +1,65 @@
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
+
4
+ # Function to process and display the images in grid format
5
+ def create_collage(uploaded_files, grid_layout):
6
+ # Open images
7
+ images = [Image.open(file.name) for file in uploaded_files]
8
+
9
+ # Determine grid dimensions based on layout
10
+ if grid_layout == "1x2":
11
+ rows, cols = 1, 2
12
+ elif grid_layout == "1x3":
13
+ rows, cols = 1, 3
14
+ elif grid_layout == "2x2":
15
+ rows, cols = 2, 2
16
+ elif grid_layout == "2x3":
17
+ rows, cols = 2, 3
18
+
19
+ # Create a blank canvas for the collage without spacing
20
+ collage_width = max(img.width for img in images) * cols
21
+ collage_height = max(img.height for img in images) * rows
22
+ collage = Image.new("RGB", (collage_width, collage_height))
23
+
24
+ # Resize images to fit the grid
25
+ resized_images = []
26
+ for img in images:
27
+ # Resize or crop image to fit the grid cell size
28
+ img_width = collage_width // cols
29
+ img_height = collage_height // rows
30
+ img_resized = img.resize((img_width, img_height), Image.ANTIALIAS) # Resize to fit
31
+ resized_images.append(img_resized)
32
+
33
+ # Place images on the canvas in grid format without spacing
34
+ for i, img in enumerate(resized_images):
35
+ row = i // cols
36
+ col = i % cols
37
+ collage.paste(img, (col * img.width, row * img.height))
38
+
39
+ return collage
40
+
41
+ # Gradio Interface
42
+ with gr.Blocks() as app:
43
+ with gr.Column():
44
+ # Step 1: File upload for multiple images
45
+ uploaded_files = gr.Files(label="Upload Images", file_count="multiple", file_types=["image"])
46
+
47
+ # Step 2: Grid selection
48
+ grid_layout = gr.Radio(
49
  choices=["1x2", "1x3", "2x2", "2x3"],
50
+ label="Select Grid Layout",
51
+ value="1x2" # Default value
52
+ )
53
+
54
+ # Step 3: Display preview of the grid
55
+ output = gr.Image(label="Collage Preview")
56
+
57
+ # Step 4: Process the images when files are uploaded or grid layout is selected
58
+ uploaded_files.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
59
+ grid_layout.change(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
60
+
61
+ # Step 5: Download button for the final collage
62
+ download_button = gr.Button("Download Collage")
63
+ download_button.click(create_collage, inputs=[uploaded_files, grid_layout], outputs=output)
64
+
65
+ app.launch()