cr8 commited on
Commit
d860b1d
·
verified ·
1 Parent(s): edf79e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -1
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from PIL import Image
3
  import rembg
4
  import io
5
- import tempfile
6
 
7
  def process_image(image, outline_size, outline_color, add_outline):
8
  if image is None:
@@ -45,6 +44,62 @@ def process_image(image, outline_size, outline_color, add_outline):
45
  final_img.save(output_buffer, format="PNG")
46
  output_buffer.seek(0)
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  return final_img, output_buffer
49
 
50
  # Gradio Interface
 
2
  from PIL import Image
3
  import rembg
4
  import io
 
5
 
6
  def process_image(image, outline_size, outline_color, add_outline):
7
  if image is None:
 
44
  final_img.save(output_buffer, format="PNG")
45
  output_buffer.seek(0)
46
 
47
+ return final_img
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 optionally add an outline.")
53
+
54
+ with gr.Row():
55
+ with gr.Column():
56
+ image_upload = gr.Image(label="Upload Image", type="pil")
57
+ add_outline = gr.Checkbox(label="Add Outline", value=True)
58
+ outline_size = gr.Slider(
59
+ label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
60
+ )
61
+ outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
62
+
63
+ with gr.Column():
64
+ output_image = gr.Image(
65
+ label="Preview and Download", type="pil", interactive=False
66
+ )
67
+
68
+ # Show/hide outline options based on checkbox
69
+ add_outline.change(
70
+ fn=lambda x: gr.update(visible=x),
71
+ inputs=add_outline,
72
+ outputs=[outline_size, outline_color],
73
+ )
74
+
75
+ # Process image and show result in the preview pane
76
+ def process_and_return(image, outline_size, outline_color, add_outline):
77
+ processed_image = process_image(image, outline_size, outline_color, add_outline)
78
+ return processed_image # Return processed image for preview
79
+
80
+ create_btn = gr.Button("Create Sticker", variant="primary")
81
+ create_btn.click(
82
+ fn=process_and_return,
83
+ inputs=[image_upload, outline_size, outline_color, add_outline],
84
+ outputs=output_image,
85
+ )
86
+
87
+ # Enable PNG download directly from the preview pane
88
+ output_image.download(label="Download Sticker as PNG")
89
+
90
+ if __name__ == "__main__":
91
+ interface.launch(debug=True)
92
+ # Paste the processed image onto the background
93
+ background.paste(processed_img, (outline_size, outline_size), processed_img)
94
+ final_img = background
95
+ else:
96
+ final_img = processed_img
97
+
98
+ # Save the result as bytes for Gradio
99
+ output_buffer = io.BytesIO()
100
+ final_img.save(output_buffer, format="PNG")
101
+ output_buffer.seek(0)
102
+
103
  return final_img, output_buffer
104
 
105
  # Gradio Interface