cr8 commited on
Commit
3d18dcc
·
verified ·
1 Parent(s): 294296d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -93
app.py CHANGED
@@ -1,99 +1,66 @@
1
- import gradio as gr
2
  import random
3
  from PIL import Image
4
 
5
- # Function to process and display the images in a comic-style grid
6
- def create_collage(uploaded_files, grid_layout, orientation):
7
- # Open images
8
- images = [Image.open(file.name) for file in uploaded_files]
9
-
10
- # Define basic grid 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
- if orientation == "Vertical":
17
- rows, cols = 2, 2 # Vertical layout
18
- else:
19
- rows, cols = 1, 4 # Horizontal layout (single row)
20
- elif grid_layout == "2x3":
21
- if orientation == "Vertical":
22
- rows, cols = 2, 3 # Vertical layout
23
- else:
24
- rows, cols = 1, 6 # Horizontal layout (single row)
25
-
26
- # Create a blank canvas for the collage without spacing
27
- collage_width = max(img.width for img in images) * cols
28
- collage_height = max(img.height for img in images) * rows
29
- collage = Image.new("RGB", (collage_width, collage_height))
30
-
31
- # Resize or randomly fit images
32
- resized_images = []
33
- for img in images:
34
- # Random width and height (mimicking comic strip style)
35
- img_width = random.randint(collage_width // (cols + 2), collage_width // cols)
36
- img_height = random.randint(collage_height // (rows + 2), collage_height // rows)
37
- img_resized = img.resize((img_width, img_height), Image.Resampling.LANCZOS) # Resize to fit
38
- resized_images.append(img_resized)
39
-
40
- # Randomly place images on the canvas (imitating a comic strip)
41
- x_offset = 0
42
- y_offset = 0
43
- for i, img in enumerate(resized_images):
44
- row = i // cols
45
- col = i % cols
46
-
47
- # Ensure the random range is valid by checking image dimensions
48
- x_pos = random.randint(x_offset, collage_width - img.width) if collage_width - img.width > x_offset else x_offset
49
- y_pos = random.randint(y_offset, collage_height - img.height) if collage_height - img.height > y_offset else y_offset
50
-
51
- collage.paste(img, (x_pos, y_pos))
52
-
53
- # Update offsets for next image placement
54
- x_offset = x_pos + img.width
55
- y_offset = y_pos + img.height
 
 
 
 
56
 
57
  return collage
58
 
59
- # Gradio Interface
60
- with gr.Blocks() as app:
61
- with gr.Column():
62
- # Step 1: File upload for multiple images
63
- uploaded_files = gr.Files(label="Upload Images", file_count="multiple", file_types=["image"])
64
-
65
- # Step 2: Grid selection
66
- grid_layout = gr.Radio(
67
- choices=["1x2", "1x3", "2x2", "2x3"],
68
- label="Select Grid Layout",
69
- value="1x2" # Default value
70
- )
71
-
72
- # Step 3: Orientation selection for 2x2 and 2x3
73
- orientation = gr.Radio(
74
- choices=["Vertical", "Horizontal"],
75
- label="Select Orientation",
76
- value="Vertical", # Default value
77
- visible=False # Initially hidden, will only show for 2x2 and 2x3
78
- )
79
-
80
- # Show orientation options only when grid layout is 2x2 or 2x3
81
- grid_layout.change(
82
- lambda layout: gr.update(visible=layout in ["2x2", "2x3"]),
83
- inputs=[grid_layout],
84
- outputs=[orientation]
85
- )
86
-
87
- # Step 4: Display preview of the grid
88
- output = gr.Image(label="Collage Preview")
89
-
90
- # Step 5: Process the images when files are uploaded or grid layout is selected
91
- uploaded_files.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
92
- grid_layout.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
93
- orientation.change(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
94
-
95
- # Step 6: Download button for the final collage
96
- download_button = gr.Button("Download Collage")
97
- download_button.click(create_collage, inputs=[uploaded_files, grid_layout, orientation], outputs=output)
98
-
99
- app.launch(share=True) # Public link to share the app
 
 
1
  import random
2
  from PIL import Image
3
 
4
+ def create_collage(images, layout):
5
+ collage_width = 1000 # Width of the final collage
6
+ collage_height = 1000 # Height of the final collage
7
+ img_width = collage_width // 2 # Set image width based on grid size
8
+ img_height = collage_height // 2 # Set image height based on grid size
9
+
10
+ if layout == '1x2': # Horizontal grid with 2 images
11
+ grid_cols = 2 # 2 images horizontally
12
+ grid_rows = 1 # 1 image vertically
13
+ img_width = collage_width // grid_cols
14
+ img_height = collage_height // grid_rows
15
+
16
+ collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
17
+
18
+ for i in range(len(images)):
19
+ img = images[i]
20
+ img_resized = img.resize((img_width, img_height), Image.LANCZOS)
21
+ x_pos = (i % grid_cols) * img_width
22
+ y_pos = (i // grid_cols) * img_height
23
+ collage.paste(img_resized, (x_pos, y_pos))
24
+
25
+ elif layout == '1x3': # Horizontal grid with 3 images
26
+ grid_cols = 3 # 3 images horizontally
27
+ grid_rows = 1 # 1 image vertically
28
+ img_width = collage_width // grid_cols
29
+ img_height = collage_height // grid_rows
30
+
31
+ collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
32
+
33
+ for i in range(len(images)):
34
+ img = images[i]
35
+ img_resized = img.resize((img_width, img_height), Image.LANCZOS)
36
+ x_pos = (i % grid_cols) * img_width
37
+ y_pos = (i // grid_cols) * img_height
38
+ collage.paste(img_resized, (x_pos, y_pos))
39
+
40
+ elif layout == '2x2' or layout == '2x3': # Random layout with 2x2 or 2x3
41
+ grid_cols = 2 # 2 images horizontally
42
+ grid_rows = 2 if layout == '2x2' else 3 # 2 or 3 images vertically
43
+ img_width = collage_width // grid_cols
44
+ img_height = collage_height // grid_rows
45
+
46
+ collage = Image.new('RGB', (collage_width, collage_height), (255, 255, 255))
47
+
48
+ # Shuffle images randomly
49
+ random.shuffle(images)
50
+
51
+ for i in range(len(images)):
52
+ img = images[i]
53
+ img_resized = img.resize((img_width, img_height), Image.LANCZOS)
54
+ row = i // grid_cols
55
+ col = i % grid_cols
56
+ x_pos = col * img_width
57
+ y_pos = row * img_height
58
+ collage.paste(img_resized, (x_pos, y_pos))
59
 
60
  return collage
61
 
62
+ # Example usage
63
+ # Assuming 'images' is a list of loaded PIL Image objects
64
+ layout = '2x3' # or '1x2', '1x3', '2x2'
65
+ collage = create_collage(images, layout)
66
+ collage.show()