cr8 commited on
Commit
2e6285a
·
verified ·
1 Parent(s): 6c1e018

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -25
app.py CHANGED
@@ -1,42 +1,63 @@
1
  import gradio as gr
2
- from PIL import Image
3
  import rembg
4
  import io
5
  import zipfile
6
 
7
  def remove_background(image):
8
- """Remove background and return transparent PNG"""
9
  img_byte_arr = io.BytesIO()
10
  image.save(img_byte_arr, format='PNG')
11
- result_bytes = rembg.remove(img_byte_arr.getvalue())
12
- return Image.open(io.BytesIO(result_bytes)).convert("RGBA")
13
 
14
- def process_batch(image_files):
15
- """Process batch of images and return ZIP of transparent PNGs"""
16
- zip_buffer = io.BytesIO()
17
- with zipfile.ZipFile(zip_buffer, "w") as zip_file:
18
- for i, img_file in enumerate(image_files):
19
- # Open and process image
20
- img = Image.open(img_file).convert("RGBA")
21
- processed_img = remove_background(img)
22
- # Save to in-memory buffer
23
- img_byte_arr = io.BytesIO()
24
- processed_img.save(img_byte_arr, format='PNG')
25
- # Add to ZIP
26
- zip_file.writestr(f"processed_{i+1}.png", img_byte_arr.getvalue())
27
- zip_buffer.seek(0)
28
- return zip_buffer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Gradio Interface
31
- image_upload = gr.Files(label="Upload Images (Batch)", file_types=["image"])
32
- output_zip = gr.File(label="Download Processed Images (ZIP)")
33
 
34
  interface = gr.Interface(
35
- fn=process_batch,
36
  inputs=image_upload,
37
- outputs=output_zip,
38
- title="Transparent Background Remover (Batch)",
39
- description="Upload multiple images, remove backgrounds, and download as transparent PNGs (batch).",
40
  )
41
 
42
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from PIL import Image, ImageChops, ImageFilter
3
  import rembg
4
  import io
5
  import zipfile
6
 
7
  def remove_background(image):
8
+ """Remove background using rembg"""
9
  img_byte_arr = io.BytesIO()
10
  image.save(img_byte_arr, format='PNG')
11
+ result = rembg.remove(img_byte_arr.getvalue())
12
+ return Image.open(io.BytesIO(result)).convert("RGBA")
13
 
14
+ def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):
15
+ """Add white outline to transparent image"""
16
+ alpha = image.split()[-1]
17
+ dilated_alpha = alpha.filter(ImageFilter.MaxFilter(outline_width * 2 + 1))
18
+ outline_mask = ImageChops.difference(dilated_alpha, alpha)
19
+
20
+ # Create outline layer
21
+ outline = Image.new('RGBA', image.size, (*outline_color, 255))
22
+ outline.putalpha(outline_mask)
23
+
24
+ # Combine layers
25
+ final = Image.alpha_composite(outline, image)
26
+ return final
27
+
28
+ def process_images(image_files):
29
+ processed = []
30
+ for img_file in image_files:
31
+ img = Image.open(img_file).convert("RGBA")
32
+ no_bg = remove_background(img)
33
+ sticker = add_outline(no_bg)
34
+ processed.append(sticker)
35
+
36
+ # Return single PNG or ZIP
37
+ if len(processed) == 1:
38
+ buf = io.BytesIO()
39
+ processed[0].save(buf, format='PNG')
40
+ return buf.getvalue()
41
+ else:
42
+ zip_buf = io.BytesIO()
43
+ with zipfile.ZipFile(zip_buf, 'w') as zip_file:
44
+ for i, sticker in enumerate(processed):
45
+ buf = io.BytesIO()
46
+ sticker.save(buf, format='PNG')
47
+ zip_file.writestr(f'sticker_{i+1}.png', buf.getvalue())
48
+ zip_buf.seek(0)
49
+ return zip_buf
50
 
51
  # Gradio Interface
52
+ image_upload = gr.Files(label="Upload Images (Single or Batch)", file_types=["image"])
53
+ output = gr.File(label="Download Sticker(s)")
54
 
55
  interface = gr.Interface(
56
+ fn=process_images,
57
  inputs=image_upload,
58
+ outputs=output,
59
+ title=" Sticker Maker",
60
+ description="Remove background, add white outline, and download as PNG(s)",
61
  )
62
 
63
  if __name__ == "__main__":