Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ 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,14 +24,6 @@ def process_image(image, outline_size, outline_color):
|
|
| 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,7 +32,7 @@ def process_image(image, outline_size, outline_color):
|
|
| 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] =
|
| 44 |
|
| 45 |
# Overlay original image
|
| 46 |
outlined_img = Image.alpha_composite(outlined_img, processed_img)
|
|
@@ -62,7 +54,7 @@ def process_image(image, outline_size, outline_color):
|
|
| 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
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column():
|
|
@@ -70,14 +62,9 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 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(
|
| 81 |
# Use HTML to create an image and a download link
|
| 82 |
image_output_html = gr.HTML(
|
| 83 |
"""
|
|
@@ -88,9 +75,9 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 88 |
"""
|
| 89 |
)
|
| 90 |
|
| 91 |
-
def update_image(image, outline_size
|
| 92 |
try:
|
| 93 |
-
data_url = process_image(image, outline_size
|
| 94 |
if data_url:
|
| 95 |
return f"""
|
| 96 |
<div style="position: relative;">
|
|
@@ -113,9 +100,10 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 113 |
</div>
|
| 114 |
"""
|
| 115 |
|
|
|
|
| 116 |
create_btn.click(
|
| 117 |
fn=update_image,
|
| 118 |
-
inputs=[image_upload, outline_size
|
| 119 |
outputs=image_output_html,
|
| 120 |
)
|
| 121 |
|
|
|
|
| 4 |
import io
|
| 5 |
import base64
|
| 6 |
|
| 7 |
+
def process_image(image, outline_size):
|
| 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 |
# 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 |
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] = (255, 255, 255, 255) # White outline
|
| 36 |
|
| 37 |
# Overlay original image
|
| 38 |
outlined_img = Image.alpha_composite(outlined_img, processed_img)
|
|
|
|
| 54 |
# Gradio Interface
|
| 55 |
with gr.Blocks(title="Sticker Maker") as interface:
|
| 56 |
gr.Markdown("# Sticker Maker")
|
| 57 |
+
gr.Markdown("Upload an image to remove the background and add a white outline.")
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column():
|
|
|
|
| 62 |
outline_size = gr.Slider(
|
| 63 |
label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
|
| 64 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 66 |
|
| 67 |
+
with gr.Column():
|
| 68 |
# Use HTML to create an image and a download link
|
| 69 |
image_output_html = gr.HTML(
|
| 70 |
"""
|
|
|
|
| 75 |
"""
|
| 76 |
)
|
| 77 |
|
| 78 |
+
def update_image(image, outline_size):
|
| 79 |
try:
|
| 80 |
+
data_url = process_image(image, outline_size)
|
| 81 |
if data_url:
|
| 82 |
return f"""
|
| 83 |
<div style="position: relative;">
|
|
|
|
| 100 |
</div>
|
| 101 |
"""
|
| 102 |
|
| 103 |
+
|
| 104 |
create_btn.click(
|
| 105 |
fn=update_image,
|
| 106 |
+
inputs=[image_upload, outline_size],
|
| 107 |
outputs=image_output_html,
|
| 108 |
)
|
| 109 |
|