cr8 commited on
Commit
bf8eb41
·
verified ·
1 Parent(s): 79f0009

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -4,14 +4,14 @@ import rembg
4
  import io
5
  import base64
6
 
7
- def process_image(image, outline_size):
8
  if image is None:
9
  return None
10
 
11
  try:
12
  # Convert image to bytes for rembg
13
  img_byte_arr = io.BytesIO()
14
- image.save(img_byte_arr, format="PNG")
15
  img_byte_arr.seek(0)
16
 
17
  # Remove background
@@ -24,6 +24,14 @@ def process_image(image, outline_size):
24
  blurred = alpha.filter(ImageFilter.GaussianBlur(radius=outline_size))
25
  new_alpha = blurred.point(lambda x: 0 if x == 0 else 255) # Binary mask
26
 
 
 
 
 
 
 
 
 
27
  # Apply outline to the transparent image
28
  outlined_img = Image.new("RGBA", processed_img.size, (0, 0, 0, 0)) # Fully transparent
29
  pixels = outlined_img.load()
@@ -32,7 +40,7 @@ def process_image(image, outline_size):
32
  for x in range(outlined_img.width):
33
  for y in range(outlined_img.height):
34
  if alpha_pixels[x, y] > 0:
35
- pixels[x, y] = (255, 255, 255, 255) # White outline
36
 
37
  # Overlay original image
38
  outlined_img = Image.alpha_composite(outlined_img, processed_img)
@@ -40,7 +48,7 @@ def process_image(image, outline_size):
40
 
41
  # Save the result as bytes for Gradio
42
  output_buffer = io.BytesIO()
43
- outlined_img.save(output_buffer, format="PNG")
44
  output_buffer.seek(0)
45
  img_data = base64.b64encode(output_buffer.read()).decode("utf-8")
46
  data_url = f"data:image/png;base64,{img_data}"
@@ -62,6 +70,11 @@ with gr.Blocks(title="Sticker Maker") as interface:
62
  outline_size = gr.Slider(
63
  label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
64
  )
 
 
 
 
 
65
  create_btn = gr.Button("Create Sticker", variant="primary")
66
 
67
  with gr.Column():
@@ -75,9 +88,9 @@ with gr.Blocks(title="Sticker Maker") as interface:
75
  """
76
  )
77
 
78
- def update_image(image, outline_size):
79
  try:
80
- data_url = process_image(image, outline_size)
81
  if data_url:
82
  return f"""
83
  <div style="position: relative;">
@@ -100,10 +113,9 @@ with gr.Blocks(title="Sticker Maker") as interface:
100
  </div>
101
  """
102
 
103
-
104
  create_btn.click(
105
  fn=update_image,
106
- inputs=[image_upload, outline_size],
107
  outputs=image_output_html,
108
  )
109
 
 
4
  import io
5
  import base64
6
 
7
+ def process_image(image, outline_size, outline_color):
8
  if image is None:
9
  return None
10
 
11
  try:
12
  # Convert image to bytes for rembg
13
  img_byte_arr = io.BytesIO()
14
+ image.save(img_byte_arr, format="PNG", dpi=(300, 300)) # Set DPI here
15
  img_byte_arr.seek(0)
16
 
17
  # Remove background
 
24
  blurred = alpha.filter(ImageFilter.GaussianBlur(radius=outline_size))
25
  new_alpha = blurred.point(lambda x: 0 if x == 0 else 255) # Binary mask
26
 
27
+ # Define outline color based on selection
28
+ if outline_color == "white":
29
+ outline_rgb = (255, 255, 255, 255) # White
30
+ elif outline_color == "black":
31
+ outline_rgb = (0, 0, 0, 255) # Black
32
+ else:
33
+ outline_rgb = (255, 255, 255, 255) #Default white
34
+
35
  # Apply outline to the transparent image
36
  outlined_img = Image.new("RGBA", processed_img.size, (0, 0, 0, 0)) # Fully transparent
37
  pixels = outlined_img.load()
 
40
  for x in range(outlined_img.width):
41
  for y in range(outlined_img.height):
42
  if alpha_pixels[x, y] > 0:
43
+ pixels[x, y] = outline_rgb
44
 
45
  # Overlay original image
46
  outlined_img = Image.alpha_composite(outlined_img, processed_img)
 
48
 
49
  # Save the result as bytes for Gradio
50
  output_buffer = io.BytesIO()
51
+ outlined_img.save(output_buffer, format="PNG", dpi=(300, 300)) # Set DPI here
52
  output_buffer.seek(0)
53
  img_data = base64.b64encode(output_buffer.read()).decode("utf-8")
54
  data_url = f"data:image/png;base64,{img_data}"
 
70
  outline_size = gr.Slider(
71
  label="Outline Thickness", minimum=0, maximum=20, value=5, step=1
72
  )
73
+ outline_color = gr.Radio(
74
+ choices=["white", "black"],
75
+ value="white",
76
+ label="Outline Color",
77
+ )
78
  create_btn = gr.Button("Create Sticker", variant="primary")
79
 
80
  with gr.Column():
 
88
  """
89
  )
90
 
91
+ def update_image(image, outline_size, outline_color):
92
  try:
93
+ data_url = process_image(image, outline_size, outline_color)
94
  if data_url:
95
  return f"""
96
  <div style="position: relative;">
 
113
  </div>
114
  """
115
 
 
116
  create_btn.click(
117
  fn=update_image,
118
+ inputs=[image_upload, outline_size, outline_color],
119
  outputs=image_output_html,
120
  )
121