Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,43 +8,48 @@ def process_image(image, outline_size):
|
|
| 8 |
if image is None:
|
| 9 |
return None
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
for
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
with gr.Blocks(title="Sticker Maker") as interface:
|
|
@@ -71,22 +76,25 @@ with gr.Blocks(title="Sticker Maker") as interface:
|
|
| 71 |
)
|
| 72 |
|
| 73 |
def update_image(image, outline_size):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
<
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
<
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
create_btn.click(
|
| 92 |
fn=update_image,
|
|
|
|
| 8 |
if image is None:
|
| 9 |
return None
|
| 10 |
|
| 11 |
+
try:
|
| 12 |
+
# Convert image to bytes for rembg
|
| 13 |
+
img_byte_arr = io.BytesIO()
|
| 14 |
+
image.save(img_byte_arr, format="PNG")
|
| 15 |
+
img_byte_arr.seek(0)
|
| 16 |
+
|
| 17 |
+
# Remove background
|
| 18 |
+
processed_bytes = rembg.remove(img_byte_arr.getvalue())
|
| 19 |
+
processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
|
| 20 |
+
|
| 21 |
+
# Create outline mask
|
| 22 |
+
alpha = processed_img.split()[-1]
|
| 23 |
+
alpha = Image.merge('L', [alpha])
|
| 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()
|
| 30 |
+
|
| 31 |
+
alpha_pixels = new_alpha.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)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Save the result as bytes for Gradio
|
| 42 |
+
output_buffer = io.BytesIO()
|
| 43 |
+
outlined_img.save(output_buffer, format="PNG")
|
| 44 |
+
output_buffer.seek(0)
|
| 45 |
+
img_data = base64.b64encode(output_buffer.read()).decode("utf-8")
|
| 46 |
+
data_url = f"data:image/png;base64,{img_data}"
|
| 47 |
+
|
| 48 |
+
return data_url
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Error processing image: {e}") # Log the error
|
| 52 |
+
return None
|
| 53 |
|
| 54 |
# Gradio Interface
|
| 55 |
with gr.Blocks(title="Sticker Maker") as interface:
|
|
|
|
| 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;">
|
| 84 |
+
<img id="sticker-preview" src="{data_url}" style="max-width: 100%; max-height: 400px;">
|
| 85 |
+
<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>
|
| 86 |
+
</div>
|
| 87 |
+
"""
|
| 88 |
+
else:
|
| 89 |
+
return """
|
| 90 |
+
<div style="position: relative;">
|
| 91 |
+
<img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;">
|
| 92 |
+
<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>
|
| 93 |
+
</div>
|
| 94 |
+
"""
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"Error in update_image: {e}") # Log the error
|
| 97 |
+
return f"Error: {e}" # Display error message in preview pane
|
| 98 |
|
| 99 |
create_btn.click(
|
| 100 |
fn=update_image,
|