Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ from PIL import Image
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
|
|
|
|
|
|
|
| 7 |
# Background removal function
|
| 8 |
def remove_background(image):
|
| 9 |
input_bytes = io.BytesIO()
|
|
@@ -12,11 +14,9 @@ def remove_background(image):
|
|
| 12 |
|
| 13 |
output_bytes = remove(input_bytes)
|
| 14 |
output_img = Image.open(io.BytesIO(output_bytes))
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
output_img.save(output_path, format="PNG")
|
| 18 |
-
|
| 19 |
-
return output_path, "Background removed successfully!"
|
| 20 |
|
| 21 |
# Example images
|
| 22 |
example_images = [
|
|
@@ -43,13 +43,10 @@ with gr.Blocks(title="Background Remover") as demo:
|
|
| 43 |
with gr.Column():
|
| 44 |
output_image = gr.Image(label="Output", type="filepath")
|
| 45 |
output_text = gr.Textbox(label="Status", lines=1)
|
| 46 |
-
|
| 47 |
-
with download_area:
|
| 48 |
-
gr.Markdown("### Download Output")
|
| 49 |
-
download_output = gr.File(label="", file_types=[".png"])
|
| 50 |
|
| 51 |
with gr.Row():
|
| 52 |
-
gr.Markdown("###
|
| 53 |
with gr.Row():
|
| 54 |
gr.Examples(
|
| 55 |
examples=example_images,
|
|
@@ -57,12 +54,25 @@ with gr.Blocks(title="Background Remover") as demo:
|
|
| 57 |
label="Click to try an example",
|
| 58 |
)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
def process(img):
|
| 62 |
output_path, msg = remove_background(img)
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
submit_btn.click(fn=process, inputs=image_input, outputs=[output_image, output_text,
|
| 66 |
-
clear_btn.click(fn=lambda: (None, "", gr.update(visible=False
|
| 67 |
|
| 68 |
demo.launch()
|
|
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
OUTPUT_FILE = "output.png"
|
| 8 |
+
|
| 9 |
# Background removal function
|
| 10 |
def remove_background(image):
|
| 11 |
input_bytes = io.BytesIO()
|
|
|
|
| 14 |
|
| 15 |
output_bytes = remove(input_bytes)
|
| 16 |
output_img = Image.open(io.BytesIO(output_bytes))
|
| 17 |
+
output_img.save(OUTPUT_FILE, format="PNG")
|
| 18 |
|
| 19 |
+
return OUTPUT_FILE, "Background removed successfully!"
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Example images
|
| 22 |
example_images = [
|
|
|
|
| 43 |
with gr.Column():
|
| 44 |
output_image = gr.Image(label="Output", type="filepath")
|
| 45 |
output_text = gr.Textbox(label="Status", lines=1)
|
| 46 |
+
download_html = gr.HTML(visible=False)
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
+
gr.Markdown("### Example Images")
|
| 50 |
with gr.Row():
|
| 51 |
gr.Examples(
|
| 52 |
examples=example_images,
|
|
|
|
| 54 |
label="Click to try an example",
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Logic
|
| 58 |
def process(img):
|
| 59 |
output_path, msg = remove_background(img)
|
| 60 |
+
download_button = f"""
|
| 61 |
+
<a href="file/{output_path}" download style="
|
| 62 |
+
display:inline-block;
|
| 63 |
+
padding: 10px 20px;
|
| 64 |
+
background-color: #4CAF50;
|
| 65 |
+
color: white;
|
| 66 |
+
text-align: center;
|
| 67 |
+
text-decoration: none;
|
| 68 |
+
border-radius: 5px;
|
| 69 |
+
font-weight: bold;
|
| 70 |
+
margin-top: 10px;
|
| 71 |
+
">⬇ Download Output</a>
|
| 72 |
+
"""
|
| 73 |
+
return output_path, msg, gr.update(visible=True, value=download_button)
|
| 74 |
|
| 75 |
+
submit_btn.click(fn=process, inputs=image_input, outputs=[output_image, output_text, download_html])
|
| 76 |
+
clear_btn.click(fn=lambda: (None, "", gr.update(visible=False, value="")), outputs=[image_input, output_text, download_html])
|
| 77 |
|
| 78 |
demo.launch()
|