cr8 commited on
Commit
e73c8b7
·
verified ·
1 Parent(s): dd5db0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -31
app.py CHANGED
@@ -1,17 +1,16 @@
1
  import gradio as gr
2
  from PIL import Image, ImageChops, ImageFilter
3
  import rembg
4
- import io
5
- import zipfile
6
  import tempfile
7
  import os
 
8
 
9
  def remove_background(image):
10
  """Remove background using rembg"""
11
- img_byte_arr = io.BytesIO()
12
- image.save(img_byte_arr, format='PNG')
13
- result = rembg.remove(img_byte_arr.getvalue())
14
- return Image.open(io.BytesIO(result)).convert("RGBA")
15
 
16
  def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
17
  """Add white outline to transparent image"""
@@ -28,33 +27,43 @@ def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
28
  return final
29
 
30
  def process_images(image_files):
31
- processed = []
32
- for img_file in image_files:
33
- img = Image.open(img_file.name).convert("RGBA") # Use .name for Gradio file paths
34
- no_bg = remove_background(img)
35
- sticker = add_outline(no_bg)
36
- processed.append(sticker)
37
-
38
- if len(processed) == 1:
39
- # Return single PNG
40
- buf = io.BytesIO()
41
- processed[0].save(buf, format='PNG')
42
- buf.seek(0)
43
- return buf
44
- else:
45
- # Return ZIP
46
- zip_buf = io.BytesIO()
47
- with zipfile.ZipFile(zip_buf, 'w') as zip_file:
48
- for i, sticker in enumerate(processed):
49
- buf = io.BytesIO()
50
- sticker.save(buf, format='PNG')
51
- buf.seek(0)
52
- zip_file.writestr(f'sticker_{i+1}.png', buf.read())
53
- zip_buf.seek(0)
54
- return zip_buf
 
 
 
 
 
 
 
55
 
56
  # Gradio Interface
57
- image_upload = gr.File(label="Upload Images (Single or Batch)", file_types=["image"], file_count="multiple")
 
 
 
58
  output = gr.File(label="Download Sticker(s)")
59
 
60
  interface = gr.Interface(
@@ -63,6 +72,7 @@ interface = gr.Interface(
63
  outputs=output,
64
  title="✨ Sticker Maker",
65
  description="Remove background, add white outline, and download as PNG(s)",
 
66
  )
67
 
68
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from PIL import Image, ImageChops, ImageFilter
3
  import rembg
 
 
4
  import tempfile
5
  import os
6
+ import zipfile
7
 
8
  def remove_background(image):
9
  """Remove background using rembg"""
10
+ with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp:
11
+ image.save(temp.name)
12
+ result = rembg.remove(temp.name)
13
+ return Image.open(result).convert("RGBA")
14
 
15
  def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
16
  """Add white outline to transparent image"""
 
27
  return final
28
 
29
  def process_images(image_files):
30
+ # Create temporary directory
31
+ temp_dir = tempfile.mkdtemp()
32
+ try:
33
+ processed_paths = []
34
+ for i, img_file in enumerate(image_files):
35
+ # Process image
36
+ img = Image.open(img_file.name).convert("RGBA")
37
+ no_bg = remove_background(img)
38
+ sticker = add_outline(no_bg)
39
+
40
+ # Save to temporary file
41
+ file_path = os.path.join(temp_dir, f"sticker_{i+1}.png")
42
+ sticker.save(file_path)
43
+ processed_paths.append(file_path)
44
+
45
+ # Return single file or ZIP
46
+ if len(processed_paths) == 1:
47
+ return processed_paths[0]
48
+ else:
49
+ zip_path = os.path.join(temp_dir, "stickers.zip")
50
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
51
+ for p in processed_paths:
52
+ zipf.write(p, os.path.basename(p))
53
+ return zip_path
54
+ except Exception as e:
55
+ # Cleanup on error
56
+ for p in processed_paths:
57
+ try:
58
+ os.remove(p)
59
+ except: pass
60
+ raise e
61
 
62
  # Gradio Interface
63
+ image_upload = gr.File(label="Upload Images (Single or Batch)",
64
+ file_types=["image"],
65
+ file_count="multiple")
66
+
67
  output = gr.File(label="Download Sticker(s)")
68
 
69
  interface = gr.Interface(
 
72
  outputs=output,
73
  title="✨ Sticker Maker",
74
  description="Remove background, add white outline, and download as PNG(s)",
75
+ allow_flagging="never"
76
  )
77
 
78
  if __name__ == "__main__":