Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import rembg
|
| 4 |
import io
|
| 5 |
import base64
|
| 6 |
|
| 7 |
-
def process_image(image, outline_size):
|
| 8 |
if image is None:
|
| 9 |
return None
|
| 10 |
|
|
@@ -24,6 +24,14 @@ def process_image(image, outline_size):
|
|
| 24 |
blurred = alpha.filter(ImageFilter.GaussianBlur(radius=outline_size))
|
| 25 |
new_alpha = blurred.point(lambda x: 0 if x == 0 else 255) # Binary mask
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Apply outline to the transparent image
|
| 28 |
outlined_img = Image.new("RGBA", processed_img.size, (0, 0, 0, 0)) # Fully transparent
|
| 29 |
pixels = outlined_img.load()
|
|
@@ -32,11 +40,12 @@ def process_image(image, outline_size):
|
|
| 32 |
for x in range(outlined_img.width):
|
| 33 |
for y in range(outlined_img.height):
|
| 34 |
if alpha_pixels[x, y] > 0:
|
| 35 |
-
pixels[x, y] =
|
| 36 |
|
| 37 |
# Overlay original image
|
| 38 |
outlined_img = Image.alpha_composite(outlined_img, processed_img)
|
| 39 |
|
|
|
|
| 40 |
# Save the result as bytes for Gradio
|
| 41 |
output_buffer = io.BytesIO()
|
| 42 |
outlined_img.save(output_buffer, format="PNG")
|
|
@@ -53,7 +62,7 @@ def process_image(image, outline_size):
|
|
| 53 |
# Gradio Interface
|
| 54 |
with gr.Blocks(title="Sticker Maker") as interface:
|
| 55 |
gr.Markdown("# Sticker Maker")
|
| 56 |
-
gr.Markdown("Upload an image to remove the background and add
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
with gr.Column():
|
|
@@ -61,6 +70,11 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 61 |
outline_size = gr.Slider(
|
| 62 |
label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
|
| 63 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 65 |
|
| 66 |
with gr.Column(visible=True): # Ensure this column is always visible
|
|
@@ -74,9 +88,9 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 74 |
"""
|
| 75 |
)
|
| 76 |
|
| 77 |
-
def update_image(image, outline_size):
|
| 78 |
try:
|
| 79 |
-
data_url = process_image(image, outline_size)
|
| 80 |
if data_url:
|
| 81 |
return f"""
|
| 82 |
<div style="position: relative;">
|
|
@@ -101,7 +115,7 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 101 |
|
| 102 |
create_btn.click(
|
| 103 |
fn=update_image,
|
| 104 |
-
inputs=[image_upload, outline_size],
|
| 105 |
outputs=image_output_html,
|
| 106 |
)
|
| 107 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageFilter
|
| 3 |
import rembg
|
| 4 |
import io
|
| 5 |
import base64
|
| 6 |
|
| 7 |
+
def process_image(image, outline_size, outline_color):
|
| 8 |
if image is None:
|
| 9 |
return None
|
| 10 |
|
|
|
|
| 24 |
blurred = alpha.filter(ImageFilter.GaussianBlur(radius=outline_size))
|
| 25 |
new_alpha = blurred.point(lambda x: 0 if x == 0 else 255) # Binary mask
|
| 26 |
|
| 27 |
+
# Define outline color based on selection
|
| 28 |
+
if outline_color == "white":
|
| 29 |
+
outline_rgb = (255, 255, 255, 255) # White
|
| 30 |
+
elif outline_color == "black":
|
| 31 |
+
outline_rgb = (0, 0, 0, 255) # Black
|
| 32 |
+
else:
|
| 33 |
+
outline_rgb = (255, 255, 255, 255) #Default white
|
| 34 |
+
|
| 35 |
# Apply outline to the transparent image
|
| 36 |
outlined_img = Image.new("RGBA", processed_img.size, (0, 0, 0, 0)) # Fully transparent
|
| 37 |
pixels = outlined_img.load()
|
|
|
|
| 40 |
for x in range(outlined_img.width):
|
| 41 |
for y in range(outlined_img.height):
|
| 42 |
if alpha_pixels[x, y] > 0:
|
| 43 |
+
pixels[x, y] = outline_rgb
|
| 44 |
|
| 45 |
# Overlay original image
|
| 46 |
outlined_img = Image.alpha_composite(outlined_img, processed_img)
|
| 47 |
|
| 48 |
+
|
| 49 |
# Save the result as bytes for Gradio
|
| 50 |
output_buffer = io.BytesIO()
|
| 51 |
outlined_img.save(output_buffer, format="PNG")
|
|
|
|
| 62 |
# Gradio Interface
|
| 63 |
with gr.Blocks(title="Sticker Maker") as interface:
|
| 64 |
gr.Markdown("# Sticker Maker")
|
| 65 |
+
gr.Markdown("Upload an image to remove the background and add an outline.")
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column():
|
|
|
|
| 70 |
outline_size = gr.Slider(
|
| 71 |
label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
|
| 72 |
)
|
| 73 |
+
outline_color = gr.Radio(
|
| 74 |
+
choices=["white", "black"],
|
| 75 |
+
value="white",
|
| 76 |
+
label="Outline Color",
|
| 77 |
+
)
|
| 78 |
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 79 |
|
| 80 |
with gr.Column(visible=True): # Ensure this column is always visible
|
|
|
|
| 88 |
"""
|
| 89 |
)
|
| 90 |
|
| 91 |
+
def update_image(image, outline_size, outline_color):
|
| 92 |
try:
|
| 93 |
+
data_url = process_image(image, outline_size, outline_color)
|
| 94 |
if data_url:
|
| 95 |
return f"""
|
| 96 |
<div style="position: relative;">
|
|
|
|
| 115 |
|
| 116 |
create_btn.click(
|
| 117 |
fn=update_image,
|
| 118 |
+
inputs=[image_upload, outline_size, outline_color],
|
| 119 |
outputs=image_output_html,
|
| 120 |
)
|
| 121 |
|