Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import rembg
|
| 4 |
import io
|
|
|
|
| 5 |
|
| 6 |
def process_image(image, outline_size):
|
| 7 |
if image is None:
|
| 8 |
-
return None
|
| 9 |
|
| 10 |
# Convert image to bytes for rembg
|
| 11 |
img_byte_arr = io.BytesIO()
|
|
@@ -16,35 +17,34 @@ def process_image(image, outline_size):
|
|
| 16 |
processed_bytes = rembg.remove(img_byte_arr.getvalue())
|
| 17 |
processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
final_img = processed_img
|
| 41 |
-
|
| 42 |
-
# Save as PNG to a BytesIO object
|
| 43 |
output_buffer = io.BytesIO()
|
| 44 |
-
|
| 45 |
output_buffer.seek(0)
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
return
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
with gr.Blocks(title="Sticker Maker") as interface:
|
|
@@ -60,25 +60,38 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 60 |
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 61 |
|
| 62 |
with gr.Column():
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
data_url =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
# Create the download link
|
| 75 |
-
download_html = f"<a href='{data_url}' download='sticker.png'>Download Sticker</a>"
|
| 76 |
-
return processed_image, download_html
|
| 77 |
|
| 78 |
create_btn.click(
|
| 79 |
-
fn=
|
| 80 |
inputs=[image_upload, outline_size],
|
| 81 |
-
outputs=
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
|
|
|
| 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):
|
| 8 |
if image is None:
|
| 9 |
+
return None
|
| 10 |
|
| 11 |
# Convert image to bytes for rembg
|
| 12 |
img_byte_arr = io.BytesIO()
|
|
|
|
| 17 |
processed_bytes = rembg.remove(img_byte_arr.getvalue())
|
| 18 |
processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
|
| 19 |
|
| 20 |
+
# Create outline mask
|
| 21 |
+
alpha = processed_img.split()[-1]
|
| 22 |
+
alpha = Image.merge('L', [alpha])
|
| 23 |
+
blurred = alpha.filter(ImageFilter.GaussianBlur(radius=outline_size))
|
| 24 |
+
new_alpha = blurred.point(lambda x: 0 if x == 0 else 255) # Binary mask
|
| 25 |
+
|
| 26 |
+
# Apply outline to the transparent image
|
| 27 |
+
outlined_img = Image.new("RGBA", processed_img.size, (0, 0, 0, 0)) # Fully transparent
|
| 28 |
+
pixels = outlined_img.load()
|
| 29 |
+
|
| 30 |
+
alpha_pixels = new_alpha.load()
|
| 31 |
+
for x in range(outlined_img.width):
|
| 32 |
+
for y in range(outlined_img.height):
|
| 33 |
+
if alpha_pixels[x, y] > 0:
|
| 34 |
+
pixels[x, y] = (255, 255, 255, 255) # White outline
|
| 35 |
+
|
| 36 |
+
# Overlay original image
|
| 37 |
+
outlined_img = Image.alpha_composite(outlined_img, processed_img)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# Save the result as bytes for Gradio
|
|
|
|
|
|
|
|
|
|
| 41 |
output_buffer = io.BytesIO()
|
| 42 |
+
outlined_img.save(output_buffer, format="PNG")
|
| 43 |
output_buffer.seek(0)
|
| 44 |
+
img_data = base64.b64encode(output_buffer.read()).decode("utf-8")
|
| 45 |
+
data_url = f"data:image/png;base64,{img_data}"
|
| 46 |
|
| 47 |
+
return data_url
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
with gr.Blocks(title="Sticker Maker") as interface:
|
|
|
|
| 60 |
create_btn = gr.Button("Create Sticker", variant="primary")
|
| 61 |
|
| 62 |
with gr.Column():
|
| 63 |
+
# Use HTML to create an image and a download link
|
| 64 |
+
image_output_html = gr.HTML(
|
| 65 |
+
"""
|
| 66 |
+
<div style="position: relative;">
|
| 67 |
+
<img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;">
|
| 68 |
+
<a id="download-link" style="position: absolute; top: 10px; right: 10px; background-color: rgba(255, 255, 255, 0.7); padding: 5px; border-radius: 5px; display:none;" download="sticker.png">Download</a>
|
| 69 |
+
</div>
|
| 70 |
+
"""
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
def update_image(image, outline_size):
|
| 74 |
+
data_url = process_image(image, outline_size)
|
| 75 |
+
if data_url:
|
| 76 |
+
return f"""
|
| 77 |
+
<div style="position: relative;">
|
| 78 |
+
<img id="sticker-preview" src="{data_url}" style="max-width: 100%; max-height: 400px;">
|
| 79 |
+
<a id="download-link" href="{data_url}" download="sticker.png" style="position: absolute; top: 10px; right: 10px; background-color: rgba(255, 255, 255, 0.7); padding: 5px; border-radius: 5px;">Download</a>
|
| 80 |
+
</div>
|
| 81 |
+
"""
|
| 82 |
+
else:
|
| 83 |
+
return """
|
| 84 |
+
<div style="position: relative;">
|
| 85 |
+
<img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;">
|
| 86 |
+
<a id="download-link" style="position: absolute; top: 10px; right: 10px; background-color: rgba(255, 255, 255, 0.7); padding: 5px; border-radius: 5px; display:none;" download="sticker.png">Download</a>
|
| 87 |
+
</div>
|
| 88 |
+
"""
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
create_btn.click(
|
| 92 |
+
fn=update_image,
|
| 93 |
inputs=[image_upload, outline_size],
|
| 94 |
+
outputs=image_output_html,
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|