cr8 commited on
Commit
98bffda
·
verified ·
1 Parent(s): 90ea771

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -17
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:
@@ -40,16 +39,12 @@ def process_image(image, outline_size, outline_color, add_outline):
40
  else:
41
  final_img = processed_img
42
 
43
- return final_img
 
 
 
44
 
45
- # Handle download
46
- def download_sticker(image):
47
- if image:
48
- # Save image to a temporary file
49
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
50
- image.save(temp_file.name, format="PNG")
51
- temp_file.close()
52
- return temp_file.name # Return the file path
53
 
54
  # Gradio Interface
55
  with gr.Blocks(title="Sticker Maker") as interface:
@@ -70,7 +65,6 @@ with gr.Blocks(title="Sticker Maker") as interface:
70
  output_image = gr.Image(
71
  label="Preview and Download", type="pil", interactive=False
72
  )
73
- download_btn = gr.Button("Download Sticker", variant="secondary")
74
 
75
  # Show/hide outline options based on checkbox
76
  add_outline.change(
@@ -79,18 +73,17 @@ with gr.Blocks(title="Sticker Maker") as interface:
79
  outputs=[outline_size, outline_color],
80
  )
81
 
82
- # Process image and show result
83
  create_btn.click(
84
- fn=process_image,
85
  inputs=[image_upload, outline_size, outline_color, add_outline],
86
  outputs=output_image,
87
  )
88
 
89
- # Enable download functionality
90
- download_btn.click(
91
- fn=download_sticker,
92
  inputs=output_image,
93
- outputs=gr.File(label="Sticker.png"),
94
  )
95
 
96
  if __name__ == "__main__":
 
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:
 
39
  else:
40
  final_img = processed_img
41
 
42
+ # Save the result as bytes for Gradio
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 # Return image and buffer for download
 
 
 
 
 
 
 
48
 
49
  # Gradio Interface
50
  with gr.Blocks(title="Sticker Maker") as interface:
 
65
  output_image = gr.Image(
66
  label="Preview and Download", type="pil", interactive=False
67
  )
 
68
 
69
  # Show/hide outline options based on checkbox
70
  add_outline.change(
 
73
  outputs=[outline_size, outline_color],
74
  )
75
 
76
+ # Process image and allow direct download from preview pane
77
  create_btn.click(
78
+ fn=lambda *args: process_image(*args)[0],
79
  inputs=[image_upload, outline_size, outline_color, add_outline],
80
  outputs=output_image,
81
  )
82
 
83
+ output_image.change(
84
+ fn=lambda img: process_image(img, outline_size=0, outline_color="#FFFFFF", add_outline=False)[1],
 
85
  inputs=output_image,
86
+ outputs=gr.File(label="Download Sticker as PNG"),
87
  )
88
 
89
  if __name__ == "__main__":