cr8 commited on
Commit
ca816ff
·
verified ·
1 Parent(s): 7eeace0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -25
app.py CHANGED
@@ -3,16 +3,10 @@ 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:
8
  return None
9
 
10
- # Convert hex color to RGB
11
- r = int(outline_color[1:3], 16)
12
- g = int(outline_color[3:5], 16)
13
- b = int(outline_color[5:7], 16)
14
- rgb_color = (r, g, b)
15
-
16
  # Convert image to bytes for rembg
17
  img_byte_arr = io.BytesIO()
18
  image.save(img_byte_arr, format="PNG")
@@ -22,19 +16,27 @@ def process_image(image, outline_size, outline_color, add_outline):
22
  processed_bytes = rembg.remove(img_byte_arr.getvalue())
23
  processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
24
 
25
- # Add outline if selected
26
- if add_outline and outline_size > 0:
27
- # Create a larger background image with the outline color
28
  background = Image.new(
29
  "RGBA",
30
  (
31
  processed_img.width + 2 * outline_size,
32
  processed_img.height + 2 * outline_size,
33
  ),
34
- rgb_color + (255,),
35
  )
36
  # Paste the processed image onto the background
37
  background.paste(processed_img, (outline_size, outline_size), processed_img)
 
 
 
 
 
 
 
 
38
  final_img = background
39
  else:
40
  final_img = processed_img
@@ -49,35 +51,24 @@ def process_image(image, outline_size, outline_color, add_outline):
49
  # Gradio Interface
50
  with gr.Blocks(title="Sticker Maker") as interface:
51
  gr.Markdown("# Sticker Maker")
52
- gr.Markdown("Upload an image to remove the background and optionally add an outline.")
53
 
54
  with gr.Row():
55
  with gr.Column():
56
  image_upload = gr.Image(label="Upload Image", type="pil")
57
- add_outline = gr.Checkbox(label="Add Outline", value=True)
58
  outline_size = gr.Slider(
59
  label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
60
  )
61
- outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")
62
  create_btn = gr.Button("Create Sticker", variant="primary")
63
 
64
  with gr.Column():
65
- output_image = gr.Image(
66
- label="Preview", type="pil", interactive=False
67
- )
68
  download_file = gr.File(label="Download Sticker as PNG")
69
 
70
- # Show/hide outline options based on checkbox
71
- add_outline.change(
72
- fn=lambda x: gr.update(visible=x),
73
- inputs=add_outline,
74
- outputs=[outline_size, outline_color],
75
- )
76
-
77
  # Process image and allow direct download from preview pane
78
  create_btn.click(
79
  fn=process_image,
80
- inputs=[image_upload, outline_size, outline_color, add_outline],
81
  outputs=[output_image, download_file],
82
  )
83
 
 
3
  import rembg
4
  import io
5
 
6
+ def process_image(image, outline_size):
7
  if image is None:
8
  return None
9
 
 
 
 
 
 
 
10
  # Convert image to bytes for rembg
11
  img_byte_arr = io.BytesIO()
12
  image.save(img_byte_arr, format="PNG")
 
16
  processed_bytes = rembg.remove(img_byte_arr.getvalue())
17
  processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
18
 
19
+ # Add white outline if selected
20
+ if outline_size > 0:
21
+ # Create a larger background image with white color
22
  background = Image.new(
23
  "RGBA",
24
  (
25
  processed_img.width + 2 * outline_size,
26
  processed_img.height + 2 * outline_size,
27
  ),
28
+ (255, 255, 255, 0), # White with alpha=0 for transparency
29
  )
30
  # Paste the processed image onto the background
31
  background.paste(processed_img, (outline_size, outline_size), processed_img)
32
+
33
+ # Draw a white outline around the image
34
+ pixels = background.load()
35
+ for x in range(background.width):
36
+ for y in range(background.height):
37
+ if x < outline_size or y < outline_size or x >= background.width - outline_size or y >= background.height - outline_size:
38
+ pixels[x, y] = (255, 255, 255, 255) # White with alpha=255 for visibility
39
+
40
  final_img = background
41
  else:
42
  final_img = processed_img
 
51
  # Gradio Interface
52
  with gr.Blocks(title="Sticker Maker") as interface:
53
  gr.Markdown("# Sticker Maker")
54
+ gr.Markdown("Upload an image to remove the background and optionally add a white outline.")
55
 
56
  with gr.Row():
57
  with gr.Column():
58
  image_upload = gr.Image(label="Upload Image", type="pil")
 
59
  outline_size = gr.Slider(
60
  label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
61
  )
 
62
  create_btn = gr.Button("Create Sticker", variant="primary")
63
 
64
  with gr.Column():
65
+ output_image = gr.Image(label="Preview", type="pil", interactive=False)
 
 
66
  download_file = gr.File(label="Download Sticker as PNG")
67
 
 
 
 
 
 
 
 
68
  # Process image and allow direct download from preview pane
69
  create_btn.click(
70
  fn=process_image,
71
+ inputs=[image_upload, outline_size],
72
  outputs=[output_image, download_file],
73
  )
74