Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ 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,42 +16,40 @@ 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 |
-
# Add white outline
|
| 20 |
if outline_size > 0:
|
| 21 |
-
# Create a larger background image with white color
|
| 22 |
background = Image.new(
|
| 23 |
"RGBA",
|
| 24 |
(
|
| 25 |
processed_img.width + 2 * outline_size,
|
| 26 |
processed_img.height + 2 * outline_size,
|
| 27 |
),
|
| 28 |
-
(
|
| 29 |
)
|
| 30 |
-
# Paste the processed image onto the background
|
| 31 |
background.paste(processed_img, (outline_size, outline_size), processed_img)
|
| 32 |
-
|
| 33 |
-
# Draw
|
| 34 |
-
pixels = background.load()
|
| 35 |
for x in range(background.width):
|
| 36 |
for y in range(background.height):
|
| 37 |
-
if x < outline_size or y < outline_size or x >= background.width - outline_size or y >= background.height - outline_size:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
final_img = background
|
|
|
|
| 41 |
else:
|
| 42 |
final_img = processed_img
|
| 43 |
|
| 44 |
-
# Save
|
| 45 |
output_buffer = io.BytesIO()
|
| 46 |
final_img.save(output_buffer, format="PNG")
|
| 47 |
output_buffer.seek(0)
|
| 48 |
|
| 49 |
-
return final_img, output_buffer
|
| 50 |
|
| 51 |
# Gradio Interface
|
| 52 |
with gr.Blocks(title="Sticker Maker") as interface:
|
| 53 |
gr.Markdown("# Sticker Maker")
|
| 54 |
-
gr.Markdown("Upload an image to remove the background and
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
with gr.Column():
|
|
@@ -63,13 +61,24 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 63 |
|
| 64 |
with gr.Column():
|
| 65 |
output_image = gr.Image(label="Preview", type="pil", interactive=False)
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
# Process image and allow direct download from preview pane
|
| 69 |
create_btn.click(
|
| 70 |
-
fn=
|
| 71 |
inputs=[image_upload, outline_size],
|
| 72 |
-
outputs=[output_image,
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
|
|
|
| 5 |
|
| 6 |
def process_image(image, outline_size):
|
| 7 |
if image is None:
|
| 8 |
+
return None, None # Return None for both image and file
|
| 9 |
|
| 10 |
# Convert image to bytes for rembg
|
| 11 |
img_byte_arr = io.BytesIO()
|
|
|
|
| 16 |
processed_bytes = rembg.remove(img_byte_arr.getvalue())
|
| 17 |
processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
|
| 18 |
|
| 19 |
+
# Add white outline
|
| 20 |
if outline_size > 0:
|
|
|
|
| 21 |
background = Image.new(
|
| 22 |
"RGBA",
|
| 23 |
(
|
| 24 |
processed_img.width + 2 * outline_size,
|
| 25 |
processed_img.height + 2 * outline_size,
|
| 26 |
),
|
| 27 |
+
(0, 0, 0, 0), # Transparent background
|
| 28 |
)
|
|
|
|
| 29 |
background.paste(processed_img, (outline_size, outline_size), processed_img)
|
| 30 |
+
|
| 31 |
+
# Draw white outline
|
|
|
|
| 32 |
for x in range(background.width):
|
| 33 |
for y in range(background.height):
|
| 34 |
+
if (x < outline_size or y < outline_size or x >= background.width - outline_size or y >= background.height - outline_size):
|
| 35 |
+
if background.getpixel((x, y))[3] == 0: #Check if pixel is originally transparent
|
| 36 |
+
background.putpixel((x, y), (255, 255, 255, 255)) #Set to white if so
|
| 37 |
final_img = background
|
| 38 |
+
|
| 39 |
else:
|
| 40 |
final_img = processed_img
|
| 41 |
|
| 42 |
+
# Save as PNG to a BytesIO object
|
| 43 |
output_buffer = io.BytesIO()
|
| 44 |
final_img.save(output_buffer, format="PNG")
|
| 45 |
output_buffer.seek(0)
|
| 46 |
|
| 47 |
+
return final_img, output_buffer
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
with gr.Blocks(title="Sticker Maker") as interface:
|
| 51 |
gr.Markdown("# Sticker Maker")
|
| 52 |
+
gr.Markdown("Upload an image to remove the background and add a white outline.")
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column():
|
|
|
|
| 61 |
|
| 62 |
with gr.Column():
|
| 63 |
output_image = gr.Image(label="Preview", type="pil", interactive=False)
|
| 64 |
+
download_link = gr.HTML("<a style='display: none;' id='download-link'>Download Sticker</a>")
|
| 65 |
+
|
| 66 |
+
def update_outputs(image, outline_size):
|
| 67 |
+
processed_image, image_bytes = process_image(image, outline_size)
|
| 68 |
+
if processed_image is None:
|
| 69 |
+
return None, "<a style='display: none;' id='download-link'>Download Sticker</a>"
|
| 70 |
+
|
| 71 |
+
# Create a data URL for the image
|
| 72 |
+
data_url = f"data:image/png;base64,{image_bytes.getvalue().hex()}"
|
| 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=update_outputs,
|
| 80 |
inputs=[image_upload, outline_size],
|
| 81 |
+
outputs=[output_image, download_link],
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|