cr8 commited on
Commit
7060283
·
verified ·
1 Parent(s): 152fea3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -3,7 +3,6 @@ from PIL import Image
3
  import rembg
4
  import io
5
  import os
6
- import tempfile
7
 
8
  def process_image(image, outline_size, outline_color, add_outline):
9
  if image is None:
@@ -38,12 +37,11 @@ def process_image(image, outline_size, outline_color, add_outline):
38
  else:
39
  final_img = processed_img
40
 
41
- # Use a temporary file with .png extension to ensure correct format
42
- temp_dir = tempfile.gettempdir()
43
- output_path = os.path.join(temp_dir, "sticker.png")
44
- final_img.save(output_path, format="PNG")
45
 
46
- return output_path
47
 
48
  # Gradio Interface
49
  with gr.Blocks(title="Sticker Maker") as interface:
@@ -57,10 +55,11 @@ with gr.Blocks(title="Sticker Maker") as interface:
57
  outline_size = gr.Slider(label="Outline Thickness", minimum=0, maximum=20, value=5, step=1)
58
  outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
59
  create_btn = gr.Button("Create Sticker", variant="primary")
 
60
 
61
  with gr.Column():
62
- # Single output component for preview and download
63
- output_image = gr.Image(label="Transparent Background", type="filepath", container=False)
64
 
65
  # Make outline options visible only when add_outline is checked
66
  add_outline.change(
@@ -69,11 +68,26 @@ with gr.Blocks(title="Sticker Maker") as interface:
69
  outputs=[outline_size, outline_color]
70
  )
71
 
 
72
  create_btn.click(
73
  fn=process_image,
74
  inputs=[image_upload, outline_size, outline_color, add_outline],
75
  outputs=output_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  )
77
 
78
  if __name__ == "__main__":
79
- interface.launch()
 
3
  import rembg
4
  import io
5
  import os
 
6
 
7
  def process_image(image, outline_size, outline_color, add_outline):
8
  if image is None:
 
37
  else:
38
  final_img = processed_img
39
 
40
+ # Save as PNG
41
+ output_file = "sticker.png"
42
+ final_img.save(output_file, format="PNG")
 
43
 
44
+ return final_img
45
 
46
  # Gradio Interface
47
  with gr.Blocks(title="Sticker Maker") as interface:
 
55
  outline_size = gr.Slider(label="Outline Thickness", minimum=0, maximum=20, value=5, step=1)
56
  outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
57
  create_btn = gr.Button("Create Sticker", variant="primary")
58
+ download_btn = gr.Button("Download Sticker", visible=False)
59
 
60
  with gr.Column():
61
+ # Output image is visible from the start
62
+ output_image = gr.Image(label="Preview with Transparent Background", type="pil")
63
 
64
  # Make outline options visible only when add_outline is checked
65
  add_outline.change(
 
68
  outputs=[outline_size, outline_color]
69
  )
70
 
71
+ # Process image and show result
72
  create_btn.click(
73
  fn=process_image,
74
  inputs=[image_upload, outline_size, outline_color, add_outline],
75
  outputs=output_image
76
+ ).then(
77
+ fn=lambda: gr.update(visible=True),
78
+ inputs=None,
79
+ outputs=download_btn
80
+ )
81
+
82
+ # Handle manual download
83
+ def download_sticker():
84
+ return "sticker.png"
85
+
86
+ download_btn.click(
87
+ fn=download_sticker,
88
+ inputs=None,
89
+ outputs=gr.File(label="Download")
90
  )
91
 
92
  if __name__ == "__main__":
93
+ interface.launch(debug=True)