Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,92 +2,93 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import rembg
|
| 4 |
import io
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
def process_image(image, outline_size, outline_color, add_outline):
|
| 8 |
if image is None:
|
| 9 |
return None
|
| 10 |
-
|
| 11 |
# Convert hex color to RGB
|
| 12 |
r = int(outline_color[1:3], 16)
|
| 13 |
g = int(outline_color[3:5], 16)
|
| 14 |
b = int(outline_color[5:7], 16)
|
| 15 |
rgb_color = (r, g, b)
|
| 16 |
-
|
| 17 |
# Convert image to bytes for rembg
|
| 18 |
img_byte_arr = io.BytesIO()
|
| 19 |
-
image.save(img_byte_arr, format=
|
| 20 |
img_byte_arr.seek(0)
|
| 21 |
-
|
| 22 |
# Remove background
|
| 23 |
processed_bytes = rembg.remove(img_byte_arr.getvalue())
|
| 24 |
processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
|
| 25 |
-
|
| 26 |
# Add outline if selected
|
| 27 |
if add_outline and outline_size > 0:
|
| 28 |
# Create a larger background image with the outline color
|
| 29 |
-
background = Image.new(
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
# Paste the processed image onto the background
|
| 35 |
background.paste(processed_img, (outline_size, outline_size), processed_img)
|
| 36 |
final_img = background
|
| 37 |
else:
|
| 38 |
final_img = processed_img
|
| 39 |
-
|
| 40 |
-
# Save as PNG
|
| 41 |
-
output_file = "sticker.png"
|
| 42 |
-
final_img.save(output_file, format="PNG")
|
| 43 |
-
|
| 44 |
return final_img
|
| 45 |
|
| 46 |
# Gradio Interface
|
| 47 |
with gr.Blocks(title="Sticker Maker") as interface:
|
| 48 |
gr.Markdown("# Sticker Maker")
|
| 49 |
-
gr.Markdown("Upload an image to remove background and add
|
| 50 |
-
|
| 51 |
with gr.Row():
|
| 52 |
with gr.Column():
|
| 53 |
image_upload = gr.Image(label="Upload Image", type="pil")
|
| 54 |
add_outline = gr.Checkbox(label="Add Outline", value=True)
|
| 55 |
-
outline_size = gr.Slider(
|
|
|
|
|
|
|
| 56 |
outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
|
| 57 |
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
with gr.Column():
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
add_outline.change(
|
| 66 |
-
fn=lambda x: gr.update(visible=x),
|
| 67 |
-
inputs=add_outline,
|
| 68 |
-
outputs=[outline_size, outline_color]
|
| 69 |
)
|
| 70 |
-
|
| 71 |
# Process image and show result
|
| 72 |
create_btn.click(
|
| 73 |
fn=process_image,
|
| 74 |
inputs=[image_upload, outline_size, outline_color, add_outline],
|
| 75 |
-
outputs=output_image
|
| 76 |
-
).then(
|
| 77 |
-
fn=lambda: gr.update(visible=True),
|
| 78 |
-
inputs=None,
|
| 79 |
-
outputs=download_btn
|
| 80 |
)
|
| 81 |
-
|
| 82 |
-
# Handle
|
| 83 |
-
def download_sticker():
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
download_btn.click(
|
| 87 |
fn=download_sticker,
|
| 88 |
-
inputs=
|
| 89 |
-
outputs=gr.File(label="
|
| 90 |
)
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
-
interface.launch(debug=True)
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import rembg
|
| 4 |
import io
|
|
|
|
| 5 |
|
| 6 |
def process_image(image, outline_size, outline_color, add_outline):
|
| 7 |
if image is None:
|
| 8 |
return None
|
| 9 |
+
|
| 10 |
# Convert hex color to RGB
|
| 11 |
r = int(outline_color[1:3], 16)
|
| 12 |
g = int(outline_color[3:5], 16)
|
| 13 |
b = int(outline_color[5:7], 16)
|
| 14 |
rgb_color = (r, g, b)
|
| 15 |
+
|
| 16 |
# Convert image to bytes for rembg
|
| 17 |
img_byte_arr = io.BytesIO()
|
| 18 |
+
image.save(img_byte_arr, format="PNG")
|
| 19 |
img_byte_arr.seek(0)
|
| 20 |
+
|
| 21 |
# Remove background
|
| 22 |
processed_bytes = rembg.remove(img_byte_arr.getvalue())
|
| 23 |
processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
|
| 24 |
+
|
| 25 |
# Add outline if selected
|
| 26 |
if add_outline and outline_size > 0:
|
| 27 |
# Create a larger background image with the outline color
|
| 28 |
+
background = Image.new(
|
| 29 |
+
"RGBA",
|
| 30 |
+
(
|
| 31 |
+
processed_img.width + 2 * outline_size,
|
| 32 |
+
processed_img.height + 2 * outline_size,
|
| 33 |
+
),
|
| 34 |
+
rgb_color + (255,),
|
| 35 |
+
)
|
| 36 |
# Paste the processed image onto the background
|
| 37 |
background.paste(processed_img, (outline_size, outline_size), processed_img)
|
| 38 |
final_img = background
|
| 39 |
else:
|
| 40 |
final_img = processed_img
|
| 41 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return final_img
|
| 43 |
|
| 44 |
# Gradio Interface
|
| 45 |
with gr.Blocks(title="Sticker Maker") as interface:
|
| 46 |
gr.Markdown("# Sticker Maker")
|
| 47 |
+
gr.Markdown("Upload an image to remove the background and optionally add an outline.")
|
| 48 |
+
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
| 51 |
image_upload = gr.Image(label="Upload Image", type="pil")
|
| 52 |
add_outline = gr.Checkbox(label="Add Outline", value=True)
|
| 53 |
+
outline_size = gr.Slider(
|
| 54 |
+
label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
|
| 55 |
+
)
|
| 56 |
outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
|
| 57 |
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 58 |
+
|
|
|
|
| 59 |
with gr.Column():
|
| 60 |
+
output_image = gr.Image(
|
| 61 |
+
label="Preview and Download", type="pil", interactive=False
|
| 62 |
+
)
|
| 63 |
+
download_btn = gr.Button("Download Sticker", variant="secondary")
|
| 64 |
+
|
| 65 |
+
# Show/hide outline options based on checkbox
|
| 66 |
add_outline.change(
|
| 67 |
+
fn=lambda x: gr.update(visible=x),
|
| 68 |
+
inputs=add_outline,
|
| 69 |
+
outputs=[outline_size, outline_color],
|
| 70 |
)
|
| 71 |
+
|
| 72 |
# Process image and show result
|
| 73 |
create_btn.click(
|
| 74 |
fn=process_image,
|
| 75 |
inputs=[image_upload, outline_size, outline_color, add_outline],
|
| 76 |
+
outputs=output_image,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
)
|
| 78 |
+
|
| 79 |
+
# Handle download
|
| 80 |
+
def download_sticker(image):
|
| 81 |
+
if image:
|
| 82 |
+
img_byte_arr = io.BytesIO()
|
| 83 |
+
image.save(img_byte_arr, format="PNG")
|
| 84 |
+
img_byte_arr.seek(0)
|
| 85 |
+
return img_byte_arr
|
| 86 |
+
|
| 87 |
download_btn.click(
|
| 88 |
fn=download_sticker,
|
| 89 |
+
inputs=output_image,
|
| 90 |
+
outputs=gr.File(label="Sticker.png"),
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
+
interface.launch(debug=True)
|