Spaces:
Sleeping
Sleeping
Add app
Browse files
app.py
CHANGED
|
@@ -69,26 +69,45 @@ def concat_images(images, patch_size=256):
|
|
| 69 |
full_img = np.vstack(grid)
|
| 70 |
return Image.fromarray(full_img)
|
| 71 |
|
| 72 |
-
def
|
| 73 |
-
if
|
| 74 |
-
return gallery
|
| 75 |
|
| 76 |
-
# Add
|
| 77 |
if gallery is None:
|
| 78 |
gallery = []
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
clear_button = gr.Button("Clear All Images")
|
| 93 |
|
| 94 |
with gr.Column():
|
|
@@ -113,30 +132,22 @@ with gr.Blocks() as demo:
|
|
| 113 |
label="Patch Size"
|
| 114 |
)
|
| 115 |
|
| 116 |
-
show_result_button = gr.Button("Show Concatenated Result")
|
| 117 |
-
|
| 118 |
# Set up the event handlers
|
| 119 |
-
|
| 120 |
-
fn=
|
| 121 |
-
inputs=[
|
| 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=
|
| 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()
|
|
|
|
| 69 |
full_img = np.vstack(grid)
|
| 70 |
return Image.fromarray(full_img)
|
| 71 |
|
| 72 |
+
def process_images(files, gallery, patch_size):
|
| 73 |
+
if not files:
|
| 74 |
+
return gallery, None
|
| 75 |
|
| 76 |
+
# Add all new images to the gallery
|
| 77 |
if gallery is None:
|
| 78 |
gallery = []
|
| 79 |
+
|
| 80 |
+
for file in files:
|
| 81 |
+
try:
|
| 82 |
+
if not file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp')):
|
| 83 |
+
print(f"Skipping invalid file type: {file.name}")
|
| 84 |
+
continue
|
| 85 |
+
|
| 86 |
+
img = Image.open(file.name)
|
| 87 |
+
gallery.append((img, None))
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"Error loading image {file.name}: {e}")
|
| 90 |
+
|
| 91 |
+
# Generate the concatenated result
|
| 92 |
+
result = concat_images(gallery, patch_size)
|
| 93 |
+
|
| 94 |
+
return gallery, result
|
| 95 |
|
| 96 |
def clear_gallery():
|
| 97 |
+
return None, None
|
| 98 |
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
gr.Markdown("# Image Concatenation")
|
| 101 |
+
gr.Markdown("Upload multiple images to create a grid of concatenated images.")
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
with gr.Column():
|
| 105 |
+
file_input = gr.File(
|
| 106 |
+
label="Upload Images",
|
| 107 |
+
file_count="multiple",
|
| 108 |
+
file_types=[".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp"],
|
| 109 |
+
height=400
|
| 110 |
+
)
|
| 111 |
clear_button = gr.Button("Clear All Images")
|
| 112 |
|
| 113 |
with gr.Column():
|
|
|
|
| 132 |
label="Patch Size"
|
| 133 |
)
|
| 134 |
|
|
|
|
|
|
|
| 135 |
# Set up the event handlers
|
| 136 |
+
file_input.upload(
|
| 137 |
+
fn=process_images,
|
| 138 |
+
inputs=[file_input, image_gallery, patch_size],
|
| 139 |
+
outputs=[image_gallery, image_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
|
| 142 |
patch_size.change(
|
| 143 |
+
fn=process_images,
|
| 144 |
+
inputs=[file_input, image_gallery, patch_size],
|
| 145 |
+
outputs=[image_gallery, image_output]
|
| 146 |
)
|
| 147 |
|
| 148 |
clear_button.click(
|
| 149 |
fn=clear_gallery,
|
| 150 |
+
outputs=[image_gallery, image_output]
|
| 151 |
)
|
| 152 |
|
| 153 |
+
demo.launch()
|