cr8 commited on
Commit
152fea3
·
verified ·
1 Parent(s): e227540

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -72
app.py CHANGED
@@ -3,11 +3,11 @@ from PIL import Image
3
  import rembg
4
  import io
5
  import os
6
- import shutil
7
 
8
  def process_image(image, outline_size, outline_color, add_outline):
9
  if image is None:
10
- return None, None
11
 
12
  # Convert hex color to RGB
13
  r = int(outline_color[1:3], 16)
@@ -30,86 +30,25 @@ def process_image(image, outline_size, outline_color, add_outline):
30
  background = Image.new('RGBA',
31
  (processed_img.width + 2*outline_size,
32
  processed_img.height + 2*outline_size),
33
- (0, 0, 0, 0)) # Start with transparent background
34
-
35
- # Create outline mask
36
- outline_img = Image.new('RGBA', background.size, rgb_color + (255,))
37
 
38
  # Paste the processed image onto the background
39
- background.paste(outline_img, (0, 0), create_outline_mask(processed_img, outline_size))
40
  background.paste(processed_img, (outline_size, outline_size), processed_img)
41
  final_img = background
42
  else:
43
  final_img = processed_img
44
 
45
- # Create output directory if it doesn't exist
46
- os.makedirs("outputs", exist_ok=True)
47
-
48
- # Save as PNG with transparency preserved
49
- output_path = "outputs/sticker.png"
50
  final_img.save(output_path, format="PNG")
51
 
52
- # Create a copy for gradio to return (workaround for download issues)
53
- download_path = "outputs/download_sticker.png"
54
- shutil.copy2(output_path, download_path)
55
-
56
- return final_img, download_path
57
-
58
- def create_outline_mask(image, thickness):
59
- """Create a mask for the outline based on the image alpha channel"""
60
- # Get mask from alpha channel
61
- mask = image.split()[3]
62
- w, h = image.size
63
-
64
- expanded = Image.new('L', (w + 2*thickness, h + 2*thickness), 0)
65
- expanded.paste(mask, (thickness, thickness))
66
-
67
- # Create outline mask
68
- result = Image.new('L', expanded.size, 0)
69
- for i in range(thickness):
70
- # Expand the mask in all 8 directions for each thickness level
71
- temp = Image.new('L', expanded.size, 0)
72
- # Expand right
73
- temp.paste(expanded, (-1, 0))
74
- result = Image.composite(temp, result, temp)
75
- # Expand left
76
- temp = Image.new('L', expanded.size, 0)
77
- temp.paste(expanded, (1, 0))
78
- result = Image.composite(temp, result, temp)
79
- # Expand up
80
- temp = Image.new('L', expanded.size, 0)
81
- temp.paste(expanded, (0, 1))
82
- result = Image.composite(temp, result, temp)
83
- # Expand down
84
- temp = Image.new('L', expanded.size, 0)
85
- temp.paste(expanded, (0, -1))
86
- result = Image.composite(temp, result, temp)
87
- # Expand diagonal directions
88
- temp = Image.new('L', expanded.size, 0)
89
- temp.paste(expanded, (-1, -1))
90
- result = Image.composite(temp, result, temp)
91
- temp = Image.new('L', expanded.size, 0)
92
- temp.paste(expanded, (-1, 1))
93
- result = Image.composite(temp, result, temp)
94
- temp = Image.new('L', expanded.size, 0)
95
- temp.paste(expanded, (1, -1))
96
- result = Image.composite(temp, result, temp)
97
- temp = Image.new('L', expanded.size, 0)
98
- temp.paste(expanded, (1, 1))
99
- result = Image.composite(temp, result, temp)
100
-
101
- # Subtract the original mask to get just the outline
102
- for y in range(expanded.height):
103
- for x in range(expanded.width):
104
- if expanded.getpixel((x, y)) > 0:
105
- result.putpixel((x, y), 0)
106
-
107
- return result
108
 
109
  # Gradio Interface
110
  with gr.Blocks(title="Sticker Maker") as interface:
111
  gr.Markdown("# Sticker Maker")
112
- gr.Markdown("Remove background, add optional outline, and download as transparent PNG")
113
 
114
  with gr.Row():
115
  with gr.Column():
@@ -120,8 +59,8 @@ with gr.Blocks(title="Sticker Maker") as interface:
120
  create_btn = gr.Button("Create Sticker", variant="primary")
121
 
122
  with gr.Column():
123
- output_preview = gr.Image(label="Preview (Transparent Background)", type="pil")
124
- output_file = gr.File(label="Download PNG")
125
 
126
  # Make outline options visible only when add_outline is checked
127
  add_outline.change(
@@ -133,7 +72,7 @@ with gr.Blocks(title="Sticker Maker") as interface:
133
  create_btn.click(
134
  fn=process_image,
135
  inputs=[image_upload, outline_size, outline_color, add_outline],
136
- outputs=[output_preview, output_file]
137
  )
138
 
139
  if __name__ == "__main__":
 
3
  import rembg
4
  import io
5
  import os
6
+ import tempfile
7
 
8
  def process_image(image, outline_size, outline_color, add_outline):
9
  if image is None:
10
+ return None
11
 
12
  # Convert hex color to RGB
13
  r = int(outline_color[1:3], 16)
 
30
  background = Image.new('RGBA',
31
  (processed_img.width + 2*outline_size,
32
  processed_img.height + 2*outline_size),
33
+ rgb_color + (255,))
 
 
 
34
 
35
  # Paste the processed image onto the background
 
36
  background.paste(processed_img, (outline_size, outline_size), processed_img)
37
  final_img = background
38
  else:
39
  final_img = processed_img
40
 
41
+ # Use a temporary file with .png extension to ensure correct format
42
+ temp_dir = tempfile.gettempdir()
43
+ output_path = os.path.join(temp_dir, "sticker.png")
 
 
44
  final_img.save(output_path, format="PNG")
45
 
46
+ return output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  # Gradio Interface
49
  with gr.Blocks(title="Sticker Maker") as interface:
50
  gr.Markdown("# Sticker Maker")
51
+ gr.Markdown("Upload an image to remove background and add optional outline")
52
 
53
  with gr.Row():
54
  with gr.Column():
 
59
  create_btn = gr.Button("Create Sticker", variant="primary")
60
 
61
  with gr.Column():
62
+ # Single output component for preview and download
63
+ output_image = gr.Image(label="Transparent Background", type="filepath", container=False)
64
 
65
  # Make outline options visible only when add_outline is checked
66
  add_outline.change(
 
72
  create_btn.click(
73
  fn=process_image,
74
  inputs=[image_upload, outline_size, outline_color, add_outline],
75
+ outputs=output_image
76
  )
77
 
78
  if __name__ == "__main__":