cr8 commited on
Commit
84a0db0
·
verified ·
1 Parent(s): 19c458f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -20
app.py CHANGED
@@ -1,34 +1,119 @@
1
  import gradio as gr
2
- from PIL import Image
3
  import rembg
4
  import io
 
5
 
6
- def remove_background(image):
7
- # Convert image to bytes
 
 
 
8
  img_byte_arr = io.BytesIO()
9
  image.save(img_byte_arr, format='PNG')
 
 
 
10
  processed_bytes = rembg.remove(img_byte_arr.getvalue())
 
11
 
12
- # Prepare outputs
13
- processed_img = Image.open(io.BytesIO(processed_bytes))
14
- file_obj = io.BytesIO(processed_bytes)
15
- file_obj.name = "no_background.png" # Required for Gradio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- return processed_img, file_obj
 
 
 
 
18
 
19
- # Gradio Interface
20
- image_upload = gr.Image(label="Upload Image", type="pil")
21
- output_preview = gr.Image(label="Preview", type="pil")
22
- output_file = gr.File(label="Download PNG")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- interface = gr.Interface(
25
- fn=remove_background,
26
- inputs=image_upload,
27
- outputs=[output_preview, output_file],
28
- title=" PNG Background Remover",
29
- description="Remove background and preview/download as transparent PNG",
30
- allow_flagging="never"
31
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  if __name__ == "__main__":
34
  interface.launch()
 
1
  import gradio as gr
2
+ from PIL import Image, ImageOps
3
  import rembg
4
  import io
5
+ import numpy as np
6
 
7
+ def remove_background_add_outline(image, outline_size=5, outline_color=(255, 255, 255)):
8
+ if image is None:
9
+ return None, None
10
+
11
+ # Convert image to bytes for rembg
12
  img_byte_arr = io.BytesIO()
13
  image.save(img_byte_arr, format='PNG')
14
+ img_byte_arr.seek(0)
15
+
16
+ # Remove background
17
  processed_bytes = rembg.remove(img_byte_arr.getvalue())
18
+ processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
19
 
20
+ # Add outline
21
+ if outline_size > 0:
22
+ # Create a mask from the alpha channel
23
+ alpha = processed_img.split()[3]
24
+
25
+ # Create a slightly larger mask by dilating
26
+ larger_mask = ImageOps.expand(alpha, outline_size, fill=255)
27
+
28
+ # Create an outline image
29
+ outline_img = Image.new('RGBA', larger_mask.size, (0, 0, 0, 0))
30
+ outline_color_with_alpha = outline_color + (255,) # Add alpha to color
31
+
32
+ # Draw the outline
33
+ for x in range(larger_mask.width):
34
+ for y in range(larger_mask.height):
35
+ if larger_mask.getpixel((x, y)) == 255 and (x < outline_size or y < outline_size or
36
+ x >= processed_img.width or y >= processed_img.height or
37
+ processed_img.getpixel((x-outline_size, y-outline_size))[3] == 0):
38
+ outline_img.putpixel((x, y), outline_color_with_alpha)
39
+
40
+ # Combine the outline with the original image
41
+ final_img = Image.new('RGBA', larger_mask.size, (0, 0, 0, 0))
42
+ final_img.paste(outline_img, (0, 0), outline_img)
43
+ final_img.paste(processed_img, (outline_size, outline_size), processed_img)
44
+ else:
45
+ final_img = processed_img
46
 
47
+ # Prepare file for download
48
+ download_path = "sticker.png"
49
+ final_img.save(download_path, format="PNG")
50
+
51
+ return final_img, download_path
52
 
53
+ # Faster but simpler method for adding outline
54
+ def add_outline_simple(image, outline_size=5, outline_color=(255, 255, 255)):
55
+ if image is None:
56
+ return None, None
57
+
58
+ # Convert image to bytes for rembg
59
+ img_byte_arr = io.BytesIO()
60
+ image.save(img_byte_arr, format='PNG')
61
+ img_byte_arr.seek(0)
62
+
63
+ # Remove background
64
+ processed_bytes = rembg.remove(img_byte_arr.getvalue())
65
+ processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
66
+
67
+ # Create a larger background image with the outline color
68
+ if outline_size > 0:
69
+ background = Image.new('RGBA',
70
+ (processed_img.width + 2*outline_size,
71
+ processed_img.height + 2*outline_size),
72
+ outline_color + (255,))
73
+
74
+ # Paste the processed image onto the background
75
+ background.paste(processed_img, (outline_size, outline_size), processed_img)
76
+ final_img = background
77
+ else:
78
+ final_img = processed_img
79
+
80
+ # Prepare file for download
81
+ download_path = "sticker.png"
82
+ final_img.save(download_path, format="PNG")
83
+
84
+ return final_img, download_path
85
 
86
+ # Gradio Interface
87
+ with gr.Blocks(title="Sticker Maker") as interface:
88
+ gr.Markdown("# Sticker Maker")
89
+ gr.Markdown("Remove background, add white outline, and download as transparent PNG")
90
+
91
+ with gr.Row():
92
+ with gr.Column():
93
+ image_upload = gr.Image(label="Upload Image", type="pil")
94
+ outline_size = gr.Slider(label="Outline Thickness", minimum=0, maximum=20, value=5, step=1)
95
+ outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
96
+ create_btn = gr.Button("Create Sticker")
97
+
98
+ with gr.Column():
99
+ output_preview = gr.Image(label="Preview", type="pil")
100
+ output_file = gr.File(label="Download PNG")
101
+
102
+ def process_image(image, outline_size, outline_color):
103
+ # Convert hex color to RGB
104
+ r = int(outline_color[1:3], 16)
105
+ g = int(outline_color[3:5], 16)
106
+ b = int(outline_color[5:7], 16)
107
+ rgb_color = (r, g, b)
108
+
109
+ # Use the simpler method for better performance
110
+ return add_outline_simple(image, int(outline_size), rgb_color)
111
+
112
+ create_btn.click(
113
+ fn=process_image,
114
+ inputs=[image_upload, outline_size, outline_color],
115
+ outputs=[output_preview, output_file]
116
+ )
117
 
118
  if __name__ == "__main__":
119
  interface.launch()