Spaces:
Sleeping
Sleeping
Add application file
Browse files- app_trial.py +142 -0
- requirements.txt +3 -0
app_trial.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
def concat_images(images, patch_size=256):
|
6 |
+
if not images:
|
7 |
+
return None
|
8 |
+
|
9 |
+
# Convert images to PIL format if they aren't already
|
10 |
+
imgs = []
|
11 |
+
for img_tuple in images:
|
12 |
+
if img_tuple is None:
|
13 |
+
continue
|
14 |
+
# Gallery returns (image, caption) tuples
|
15 |
+
img = img_tuple[0] # Get the image from the tuple
|
16 |
+
if isinstance(img, str):
|
17 |
+
# If the input is a string path, load the image
|
18 |
+
img = Image.open(img)
|
19 |
+
elif isinstance(img, np.ndarray):
|
20 |
+
img = Image.fromarray(img)
|
21 |
+
imgs.append(img)
|
22 |
+
|
23 |
+
if not imgs:
|
24 |
+
return None
|
25 |
+
|
26 |
+
# Resize all images to fit within patch_size x patch_size while maintaining aspect ratio
|
27 |
+
resized_imgs = []
|
28 |
+
for img in imgs:
|
29 |
+
if img is None:
|
30 |
+
continue
|
31 |
+
# Calculate scaling factor to fit within patch_size x patch_size
|
32 |
+
width, height = img.size
|
33 |
+
scale = min(patch_size/width, patch_size/height)
|
34 |
+
new_width = int(width * scale)
|
35 |
+
new_height = int(height * scale)
|
36 |
+
|
37 |
+
# Resize image maintaining aspect ratio
|
38 |
+
resized_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
39 |
+
|
40 |
+
# Create a new white background image
|
41 |
+
padded_img = Image.new('RGB', (patch_size, patch_size), (255, 255, 255))
|
42 |
+
|
43 |
+
# Calculate position to paste the resized image (centered)
|
44 |
+
paste_x = (patch_size - new_width) // 2
|
45 |
+
paste_y = (patch_size - new_height) // 2
|
46 |
+
|
47 |
+
# Paste the resized image onto the white background
|
48 |
+
padded_img.paste(resized_img, (paste_x, paste_y))
|
49 |
+
resized_imgs.append(padded_img)
|
50 |
+
|
51 |
+
if not resized_imgs:
|
52 |
+
return None
|
53 |
+
|
54 |
+
# Calculate grid dimensions
|
55 |
+
n_images = len(resized_imgs)
|
56 |
+
row_size = int(np.ceil(np.sqrt(n_images)))
|
57 |
+
|
58 |
+
# Create the grid
|
59 |
+
grid = []
|
60 |
+
for i in range(0, n_images, row_size):
|
61 |
+
row_imgs = resized_imgs[i:i+row_size]
|
62 |
+
# Pad the last row if necessary
|
63 |
+
while len(row_imgs) < row_size:
|
64 |
+
row_imgs.append(Image.new('RGB', (patch_size, patch_size), (255, 255, 255)))
|
65 |
+
row = np.hstack([np.array(img) for img in row_imgs])
|
66 |
+
grid.append(row)
|
67 |
+
|
68 |
+
# Combine all rows
|
69 |
+
full_img = np.vstack(grid)
|
70 |
+
return Image.fromarray(full_img)
|
71 |
+
|
72 |
+
def add_to_gallery(image, gallery):
|
73 |
+
if image is None:
|
74 |
+
return gallery
|
75 |
+
|
76 |
+
# Add the new image to the gallery
|
77 |
+
if gallery is None:
|
78 |
+
gallery = []
|
79 |
+
gallery.append((image, None))
|
80 |
+
return gallery
|
81 |
+
|
82 |
+
def clear_gallery():
|
83 |
+
return None
|
84 |
+
|
85 |
+
with gr.Blocks() as demo:
|
86 |
+
gr.Markdown("# Image Concatenation")
|
87 |
+
gr.Markdown("Upload images to create a grid of concatenated images.")
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
with gr.Column():
|
91 |
+
image_input = gr.Image(type="pil", label="Upload Image", show_label=True, height=400)
|
92 |
+
clear_button = gr.Button("Clear All Images")
|
93 |
+
|
94 |
+
with gr.Column():
|
95 |
+
image_gallery = gr.Gallery(
|
96 |
+
label="Image Gallery",
|
97 |
+
show_label=True,
|
98 |
+
elem_id="gallery",
|
99 |
+
columns=[4],
|
100 |
+
rows=[4],
|
101 |
+
height=400,
|
102 |
+
allow_preview=True
|
103 |
+
)
|
104 |
+
|
105 |
+
with gr.Row():
|
106 |
+
image_output = gr.Image(type="pil", label="Result")
|
107 |
+
|
108 |
+
patch_size = gr.Slider(
|
109 |
+
minimum=256,
|
110 |
+
maximum=1024,
|
111 |
+
value=256,
|
112 |
+
step=256,
|
113 |
+
label="Patch Size"
|
114 |
+
)
|
115 |
+
|
116 |
+
show_result_button = gr.Button("Show Concatenated Result")
|
117 |
+
|
118 |
+
# Set up the event handlers
|
119 |
+
image_input.upload(
|
120 |
+
fn=add_to_gallery,
|
121 |
+
inputs=[image_input, image_gallery],
|
122 |
+
outputs=image_gallery
|
123 |
+
)
|
124 |
+
|
125 |
+
show_result_button.click(
|
126 |
+
fn=concat_images,
|
127 |
+
inputs=[image_gallery, patch_size],
|
128 |
+
outputs=image_output
|
129 |
+
)
|
130 |
+
|
131 |
+
patch_size.change(
|
132 |
+
fn=concat_images,
|
133 |
+
inputs=[image_gallery, patch_size],
|
134 |
+
outputs=image_output
|
135 |
+
)
|
136 |
+
|
137 |
+
clear_button.click(
|
138 |
+
fn=clear_gallery,
|
139 |
+
outputs=image_gallery
|
140 |
+
)
|
141 |
+
|
142 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=3.50.2
|
2 |
+
pillow
|
3 |
+
numpy
|