Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,7 +44,7 @@ def process_image(image, outline_size, outline_color, add_outline):
|
|
| 44 |
final_img.save(output_buffer, format="PNG")
|
| 45 |
output_buffer.seek(0)
|
| 46 |
|
| 47 |
-
return final_img
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
with gr.Blocks(title="Sticker Maker") as interface:
|
|
@@ -59,6 +59,7 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 59 |
label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
|
| 60 |
)
|
| 61 |
outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
|
|
|
|
| 62 |
|
| 63 |
with gr.Column():
|
| 64 |
output_image = gr.Image(
|
|
@@ -72,74 +73,17 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 72 |
outputs=[outline_size, outline_color],
|
| 73 |
)
|
| 74 |
|
| 75 |
-
# Process image and
|
| 76 |
-
def process_and_return(image, outline_size, outline_color, add_outline):
|
| 77 |
-
processed_image = process_image(image, outline_size, outline_color, add_outline)
|
| 78 |
-
return processed_image # Return processed image for preview
|
| 79 |
-
|
| 80 |
-
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 81 |
create_btn.click(
|
| 82 |
-
fn=
|
| 83 |
inputs=[image_upload, outline_size, outline_color, add_outline],
|
| 84 |
outputs=output_image,
|
| 85 |
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
interface.launch(debug=True)
|
| 92 |
-
# Paste the processed image onto the background
|
| 93 |
-
background.paste(processed_img, (outline_size, outline_size), processed_img)
|
| 94 |
-
final_img = background
|
| 95 |
-
else:
|
| 96 |
-
final_img = processed_img
|
| 97 |
-
|
| 98 |
-
# Save the result as bytes for Gradio
|
| 99 |
-
output_buffer = io.BytesIO()
|
| 100 |
-
final_img.save(output_buffer, format="PNG")
|
| 101 |
-
output_buffer.seek(0)
|
| 102 |
-
|
| 103 |
-
return final_img, output_buffer
|
| 104 |
-
|
| 105 |
-
# Gradio Interface
|
| 106 |
-
with gr.Blocks(title="Sticker Maker") as interface:
|
| 107 |
-
gr.Markdown("# Sticker Maker")
|
| 108 |
-
gr.Markdown("Upload an image to remove the background and optionally add an outline.")
|
| 109 |
-
|
| 110 |
-
with gr.Row():
|
| 111 |
-
with gr.Column():
|
| 112 |
-
image_upload = gr.Image(label="Upload Image", type="pil")
|
| 113 |
-
add_outline = gr.Checkbox(label="Add Outline", value=True)
|
| 114 |
-
outline_size = gr.Slider(
|
| 115 |
-
label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
|
| 116 |
-
)
|
| 117 |
-
outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
|
| 118 |
-
|
| 119 |
-
with gr.Column():
|
| 120 |
-
output_image = gr.Image(
|
| 121 |
-
label="Preview", type="pil", interactive=False
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
# Show/hide outline options based on checkbox
|
| 125 |
-
add_outline.change(
|
| 126 |
-
fn=lambda x: gr.update(visible=x),
|
| 127 |
-
inputs=add_outline,
|
| 128 |
-
outputs=[outline_size, outline_color],
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
# Process image and show result in the preview pane
|
| 132 |
-
def process_and_download(image, outline_size, outline_color, add_outline):
|
| 133 |
-
processed_image, output_buffer = process_image(image, outline_size, outline_color, add_outline)
|
| 134 |
-
# Return the processed image for preview and the PNG file for download
|
| 135 |
-
return processed_image, output_buffer
|
| 136 |
-
|
| 137 |
-
# Add button to process image and provide preview
|
| 138 |
-
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 139 |
-
create_btn.click(
|
| 140 |
-
fn=process_and_download,
|
| 141 |
-
inputs=[image_upload, outline_size, outline_color, add_outline],
|
| 142 |
-
outputs=[output_image, gr.File(label="Download as PNG")],
|
| 143 |
)
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
|
|
|
| 44 |
final_img.save(output_buffer, format="PNG")
|
| 45 |
output_buffer.seek(0)
|
| 46 |
|
| 47 |
+
return final_img, output_buffer # Return image and buffer for download
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
with gr.Blocks(title="Sticker Maker") as interface:
|
|
|
|
| 59 |
label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
|
| 60 |
)
|
| 61 |
outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
|
| 62 |
+
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 63 |
|
| 64 |
with gr.Column():
|
| 65 |
output_image = gr.Image(
|
|
|
|
| 73 |
outputs=[outline_size, outline_color],
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# Process image and allow direct download from preview pane
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
create_btn.click(
|
| 78 |
+
fn=lambda *args: process_image(*args)[0],
|
| 79 |
inputs=[image_upload, outline_size, outline_color, add_outline],
|
| 80 |
outputs=output_image,
|
| 81 |
)
|
| 82 |
|
| 83 |
+
output_image.change(
|
| 84 |
+
fn=lambda img: process_image(img, outline_size=0, outline_color="#FFFFFF", add_outline=False)[1],
|
| 85 |
+
inputs=output_image,
|
| 86 |
+
outputs=gr.File(label="Download Sticker as PNG"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|